1.概要
単純なJavaスイングプログラムハロー世界、一つだけのボタンがあります
2.ソース
import javax.swing.*;
public class server
{
public static void main(String[] args) {
JFrame jFrame = new JFrame("title");
JButton button = new JButton("Test button");
jFrame.add(button);//把button添加到JFrame中
jFrame.setSize(300,300);//设置JFrame大小
jFrame.setVisible(true);//设置可见,不然的话看不到
}
}
3.最初のリビジョン
?あなたは少し奇妙に感じます、全体のボタンがウィンドウいっぱい
の権利を、以下のJPanel:
import javax.swing.*;
public class server
{
public static void main(String[] args) {
JFrame jFrame = new JFrame("title");
JPanel jPanel = new JPanel();
JButton button = new JButton("Test button");
jPanel.add(button);
jFrame.setContentPane(jPanel);
jFrame.setSize(300,300);
jFrame.setVisible(true);
}
}
その後、JFrameのcontenPaneセット、のJPanelに追加のJPanel、ボタンを追加します。
結果は以下の通りです:
4.第二の変更
ああ、ハローワールドのようなビット、しかし、あなたは、左上隅にある[x]ボタンをクリックしていませんか?
ポイントの後、この事は「消失」であるが、それでもので、バックグラウンドで実行されています...
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
デフォルトの設定は、開閉動作を必要としています。
別の修飾は、そうでない時は、常に左上隅に起動するように、中央に配置されていることです。
非常に単純な、その上に1行。
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
完全なコード:
import javax.swing.*;
public class server
{
public static void main(String[] args) {
JFrame jFrame = new JFrame("title");
JPanel jPanel = new JPanel();
JButton button = new JButton("Test button");
jPanel.add(button);
jFrame.setContentPane(jPanel);
jFrame.setSize(300,300);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}