花了20分钟写了下,现在swing程序写的都很乱,希望我写的可以帮你一下,感觉写的还算规范,公司基本都是这样要求的。
package test;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
public class TestGui extends JFrame {
private JLabel msgLbl;
private JLabel startLbl;
private JLabel endLbl;
private JLabel countLbl;
//始发站
private JList startList;
private JList endList;
private DefaultListModel stationListModel;
private JRadioButton singeRdo;
private JRadioButton doubleRdo;
private JButton okBtn;;
private JButton exitBtn;
private JSpinner countSpinner;
private JTextArea suggestionTxt;
public TestGui() {
super("订票演示");
setDefaultCloseOperation(EXIT_ON_CLOSE);
initStationListModel();
initGUI();
addListeners();
setResizable(false);
centerWindow(this);
}
private void initGUI() {
msgLbl = new JLabel("填写订票信息:");
//中间的主体内容
JPanel mainContentPnl = new JPanel();
mainContentPnl.setLayout(new GridBagLayout());
mainContentPnl.setBorder(BorderFactory.createEtchedBorder());
JPanel stationPnl = new JPanel();
stationPnl.setLayout(new FlowLayout(5));
startLbl = new JLabel("出发地:");
startList = new JList(stationListModel);
startList.setSelectedIndex(0);
startList.setPreferredSize(new Dimension(60, 80));
endLbl = new JLabel("目的地:");
endList = new JList(stationListModel);
endList.setSelectedIndex(0);
endList.setPreferredSize(new Dimension(60, 80));
stationPnl.add(startLbl);
stationPnl.add(startList);
stationPnl.add(endLbl);
stationPnl.add(endList);
JPanel typePnl = new JPanel();
typePnl.setLayout(new GridBagLayout());
ButtonGroup type = new ButtonGroup();
singeRdo = new JRadioButton("单程");
singeRdo.setSelected(true);
doubleRdo = new JRadioButton("往返");
countSpinner = new JSpinner(new SpinnerNumberModel());
countLbl = new JLabel("张数:");
type.add(singeRdo);
type.add(doubleRdo);
typePnl.add(singeRdo, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
typePnl.add(doubleRdo, new GridBagConstraints(1,0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
typePnl.add(countLbl, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
typePnl.add(countSpinner, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
suggestionTxt = new JTextArea(3, 64);
JScrollPane suggestionPane = new JScrollPane(suggestionTxt);
mainContentPnl.add(stationPnl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(0, 5, 0, 5), 0, 0));
mainContentPnl.add(typePnl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
mainContentPnl.add(suggestionPane, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
//按钮放置面板
JPanel buttonPnl = new JPanel();
buttonPnl.setLayout(new FlowLayout());
okBtn = new JButton("确定");
okBtn.setActionCommand("ok");
exitBtn = new JButton("退出");
exitBtn.setActionCommand("exit");
buttonPnl.add(okBtn);
buttonPnl.add(exitBtn);
// 容器加入到框架中
Container contentPane = getContentPane();
contentPane.setLayout(new GridBagLayout());
contentPane.add(msgLbl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.NONE, new Insets(10, 10, 0, 10), 0, 0));
contentPane.add(mainContentPnl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
GridBagConstraints.BOTH, new Insets(5, 10, 0, 10), 0, 0));
contentPane.add(buttonPnl, new GridBagConstraints(0, 2, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
GridBagConstraints.NONE, new Insets(5, 10, 10, 10), 0, 0));
pack();
}
private void addListeners()
{
ActionListener btnListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
String cmd = event.getActionCommand();
if ("ok".equals(cmd))
{
String typeInfo = singeRdo.isSelected() ? "单程" : "往返";
StringBuffer msg = new StringBuffer();
msg.append("订了从");
msg.append(startList.getSelectedValue().toString()).append("到");
msg.append(endList.getSelectedValue().toString());
msg.append(typeInfo).append("票").append(countSpinner.getValue().toString()).append("张。");
msg.append("".equals(suggestionTxt.getText().trim()) ? "我没有意见。" :
"我的意见是:" + suggestionTxt.getText().trim() + "。");
JOptionPane.showMessageDialog(TestGui.this, msg.toString());
}
else if ("exit".equals(cmd))
{
System.exit(0);
}
}
};
okBtn.addActionListener(btnListener);
exitBtn.addActionListener(btnListener);
}
/**
* 初始化站点信息
*/
private void initStationListModel()
{
String[] str = { "深圳", "北京", "江西", "湖南", "重庆" };
stationListModel = new DefaultListModel();
for (int i = 0; i < str.length; i++) {
stationListModel.addElement(str[i]);
}
}
private void centerWindow(Window w){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = w.getSize();
int x = (screenSize.width - windowSize.width) / 2;
int y = (screenSize.height - windowSize.height) / 2;
w.setLocation(new Point(x, y));
}
public static void main(String[] args) {
TestGui tg = new TestGui();
tg.setVisible(true);
}
}