扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
JFrame的默认布局管理器是BorderLayout,有五个方向,TOP、LEFT、CENTER、RIGHT、BOTTOM,如果不给控件制定方向的话会都绘制到同一个默认方向CENTER,所以你最后添加的jf2把之前添加的组件都覆盖了,为每个组件指定方向即可。建议使用JPanel,使用FlowLayout。
创新互联的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括网站制作、做网站、电商网站开发、微信营销、系统平台开发。
没用过SWT的人路过,提供一个SWING:
窗口用JFrame(frame),布局为BorderLayout.
顶部为JLabel(labelTop),带图片和文字,用frame.add(labelTop, BorderLayout.NORTH)加入
左面为JPanel(panelLeft),布局为BoxLayout,参数Y_AXIS.需要在创建JPanel时设置Layout为NULL,然后再创建BoxLayout时需要把已经建立的panelLeft作为参数传进去,然后再设置panelLeft的Layout为那个BoxLayout。用frame.add(panelLeft, BorderLayout.WEST)加入
中间为一堆不同功能的JPanel,布局根据功能而定了,在点击按钮时,移除frame此时中间的panel(记录在一个自己的变量panelCenter里),然后加入按钮对应的panel,再把加入的panel存入变量panelCenter.
java做窗口的话,需要用swing技术,之后创建JFrame 等组件,即可完成窗口创建工作。
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; //定义一个窗口架构
JMenuBar mb;//定义窗口的菜单工具栏
JMenu m; //定义菜单
JMenuItem mi1;//定义菜单的内容
JMenuItem mi2; //定义菜单的内容
public MenuTest() {
initFrame();
initAction();
}
public void initFrame() {
frame = new JFrame();
mb = new JMenuBar();
m = new JMenu("学生查询");
mi1 = new JMenuItem("确认");
mi2 = new JMenuItem("取消"); m.add(mi1);
m.add(mi2);
mb.add(m);
frame.add(mb, BorderLayout.NORTH);
frame.setSize(300, 300); //设置窗口大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置退出时关闭窗口
frame.setVisible(true);//设置窗口可见
} public void initAction() {
mi1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
System.out.println("click");
JOptionPane.showMessageDialog(null, "你点击了确定按钮");
}
});
mi2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 具体实现代码根据实际要求填写
JOptionPane.showMessageDialog(null, "你点击了取消按钮");
}
});
} public static void main(String[] args) {
new MenuTest();//执行菜单创建
}}
不知道是不是你说的窗口
使用javaswing JFrame设计窗口 + 布局就可实现,,如下例(添加了详细注释):
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());//设置布局方式,BorderLayout东西南北中布局
b1 = new JButton("点击");
b1.addActionListener(this);//为按钮添加监听
c.add(b1, BorderLayout.SOUTH);//添加按钮到c容器中,并分配在容器南(下)方
jta = new JTextArea();
c.add(jta, BorderLayout.CENTER);//添加文本区到c容器中,并分配在居中位置
this.setTitle("按钮事件");//设置窗口标题
this.setSize(300, 300);//设置窗体大小
this.setVisible(true);//窗体设置为显示
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体
//常用的一种关闭窗体的方法
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
//使用判断按钮名称的方法触发事件
if("点击".equals(e.getActionCommand())) {
jta.setText("按钮被点击了!");
}
//也可以获取对象名实现判断
// if(e.getSource() == b1) {
// jta.setText("按钮使用getSource方法被点击了!");
// }
}
public static void main(String[] args) {
new JFrameTest();
}
}
计算器: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);
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流