JFrame的默認(rèn)布局管理器是BorderLayout,有五個(gè)方向,TOP、LEFT、CENTER、RIGHT、BOTTOM,如果不給控件制定方向的話會(huì)都繪制到同一個(gè)默認(rèn)方向CENTER,所以你最后添加的jf2把之前添加的組件都覆蓋了,為每個(gè)組件指定方向即可。建議使用JPanel,使用FlowLayout。
創(chuàng)新互聯(lián)的客戶(hù)來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜?,從?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶(hù)帶來(lái)驚喜。專(zhuān)業(yè)領(lǐng)域包括網(wǎng)站制作、做網(wǎng)站、電商網(wǎng)站開(kāi)發(fā)、微信營(yíng)銷(xiāo)、系統(tǒng)平臺(tái)開(kāi)發(fā)。
沒(méi)用過(guò)SWT的人路過(guò),提供一個(gè)SWING:
窗口用JFrame(frame),布局為BorderLayout.
頂部為JLabel(labelTop),帶圖片和文字,用frame.add(labelTop, BorderLayout.NORTH)加入
左面為JPanel(panelLeft),布局為BoxLayout,參數(shù)Y_AXIS.需要在創(chuàng)建JPanel時(shí)設(shè)置Layout為NULL,然后再創(chuàng)建BoxLayout時(shí)需要把已經(jīng)建立的panelLeft作為參數(shù)傳進(jìn)去,然后再設(shè)置panelLeft的Layout為那個(gè)BoxLayout。用frame.add(panelLeft, BorderLayout.WEST)加入
中間為一堆不同功能的JPanel,布局根據(jù)功能而定了,在點(diǎn)擊按鈕時(shí),移除frame此時(shí)中間的panel(記錄在一個(gè)自己的變量panelCenter里),然后加入按鈕對(duì)應(yīng)的panel,再把加入的panel存入變量panelCenter.
java做窗口的話,需要用swing技術(shù),之后創(chuàng)建JFrame 等組件,即可完成窗口創(chuàng)建工作。
package inter.frame;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;public class MenuTest { /**
* @param args
*/
JFrame frame; //定義一個(gè)窗口架構(gòu)
JMenuBar mb;//定義窗口的菜單工具欄
JMenu m; //定義菜單
JMenuItem mi1;//定義菜單的內(nèi)容
JMenuItem mi2; //定義菜單的內(nèi)容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("學(xué)生查詢(xún)");
mi1 = new JMenuItem("確認(rèn)");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //設(shè)置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置退出時(shí)關(guān)閉窗口
frame.setVisible(true);//設(shè)置窗口可見(jiàn)
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實(shí)現(xiàn)代碼根據(jù)實(shí)際要求填寫(xiě)
System.out.println("click");
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了確定按鈕");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具體實(shí)現(xiàn)代碼根據(jù)實(shí)際要求填寫(xiě)
JOptionPane.showMessageDialog(null, "你點(diǎn)擊了取消按鈕");
}
});
} public static void main(String[] args) {
new MenuTest();//執(zhí)行菜單創(chuàng)建
}}
不知道是不是你說(shuō)的窗口
使用javaswing JFrame設(shè)計(jì)窗口 + 布局就可實(shí)現(xiàn),,如下例(添加了詳細(xì)注釋?zhuān)?/p>
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class JFrameTest extends JFrame implements ActionListener {
private static final long serialVersionUID = -2829899643559384548L;
private JButton b1 = null;//按鈕
private JTextArea jta = null;//文本
public JFrameTest() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout());//設(shè)置布局方式,BorderLayout東西南北中布局
b1 = new JButton("點(diǎn)擊");
b1.addActionListener(this);//為按鈕添加監(jiān)聽(tīng)
c.add(b1, BorderLayout.SOUTH);//添加按鈕到c容器中,并分配在容器南(下)方
jta = new JTextArea();
c.add(jta, BorderLayout.CENTER);//添加文本區(qū)到c容器中,并分配在居中位置
this.setTitle("按鈕事件");//設(shè)置窗口標(biāo)題
this.setSize(300, 300);//設(shè)置窗體大小
this.setVisible(true);//窗體設(shè)置為顯示
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關(guān)閉窗體
//常用的一種關(guān)閉窗體的方法
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
//使用判斷按鈕名稱(chēng)的方法觸發(fā)事件
if("點(diǎn)擊".equals(e.getActionCommand())) {
jta.setText("按鈕被點(diǎn)擊了!");
}
//也可以獲取對(duì)象名實(shí)現(xiàn)判斷
// if(e.getSource() == b1) {
// jta.setText("按鈕使用getSource方法被點(diǎn)擊了!");
// }
}
public static void main(String[] args) {
new JFrameTest();
}
}
計(jì)算器:import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.TextField;
import java.awt.Rectangle;
import java.awt.Button;
import java.awt.Label;public class suanfa extends JFrame {private static final long serialVersionUID = 1L;private JPanel jContentPane = null;private TextField textField = null;private TextField textField1 = null;private Button button = null;private Button button1 = null;private Button button2 = null;private Button button3 = null;private Label label = null;
double awn;
/**
* This is the default constructor
*/
public suanfa() {
super();
initialize();
}/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300, 250);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
label = new Label();
label.setBounds(new Rectangle(12, 129, 90, 27));
label.setText(String.format("%.6f",awn));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getTextField(), null);
jContentPane.add(getTextField1(), null);
jContentPane.add(getButton(), null);
jContentPane.add(getButton1(), null);
jContentPane.add(getButton2(), null);
jContentPane.add(getButton3(), null);
jContentPane.add(label, null);
}
return jContentPane;
}/**
* This method initializes textField
*
* @return java.awt.TextField
*/
private TextField getTextField() {
if (textField == null) {
textField = new TextField();
textField.setBounds(new Rectangle(11, 20, 75, 32));
}
return textField;
}/**
* This method initializes textField1
*
* @return java.awt.TextField
*/
private TextField getTextField1() {
if (textField1 == null) {
textField1 = new TextField();
textField1.setBounds(new Rectangle(11, 67, 75, 32));
}
return textField1;
}/**
* This method initializes button
*
* @return java.awt.Button
*/
private Button getButton() {
if (button == null) {
button = new Button("加");
button.setBounds(new Rectangle(134, 17, 71, 29));
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) + Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}
}
});
}
return button;
}/**
* This method initializes button1
*
* @return java.awt.Button
*/
private Button getButton1() {
if (button1 == null) {
button1 = new Button("減");
button1.setBounds(new Rectangle(134, 52, 71, 29));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) - Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}}
});
}
return button1;
}/**
* This method initializes button2
*
* @return java.awt.Button
*/
private Button getButton2() {
if (button2 == null) {
button2 = new Button("乘");
button2.setBounds(new Rectangle(134, 91, 71, 29));
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) * Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}}
});
}
return button2;
}/**
* This method initializes button3
*
* @return java.awt.Button
*/
private Button getButton3() {
if (button3 == null) {
button3 = new Button("除");
button3.setBounds(new Rectangle(134, 133, 71, 29));
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
awn=Double.valueOf(textField.getText()) / Double.valueOf(textField1.getText());
label.setText(String.format("%.6f",awn));
}
catch(Exception ex){
return;
}}
});
}
return button3;
}
public static void main(String[] args)
{
new suanfa().setVisible(true);
}
}