【发布时间】:2016-02-15 13:42:43
【问题描述】:
enter code here
public class NWAHome {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public NWAHome(){
prepareGUI();
}
public static void main(String[] args){
NWAHome swingMenuDemo = new NWAHome();
swingMenuDemo.showMenuDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Produkt anlegen");
mainFrame.setSize(400,400);
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
mainFrame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("128px"),
ColumnSpec.decode("128px"),
ColumnSpec.decode("128px"),},
new RowSpec[] {
RowSpec.decode("113px"),
RowSpec.decode("113px"),
RowSpec.decode("113px"),}));
mainFrame.getContentPane().add(headerLabel, "1, 1, fill, fill");
mainFrame.getContentPane().add(controlPanel, "3, 1, 1, 2, fill, fill");
controlPanel.setLayout(new MigLayout("", "[][]", "[][][][][]"));
JButton btnNewButton_1 = new JButton("Projekt anzeigen");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
controlPanel.add(btnNewButton_1, "flowy,cell 1 0");
JButton button_1 = new JButton("Projekt anlegen");
controlPanel.add(button_1, "cell 1 0");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JButton button = new JButton("Projekt ändern");
controlPanel.add(button, "cell 1 1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JButton button_2 = new JButton("Projekt löschen");
controlPanel.add(button_2, "flowy,cell 1 2");
JButton btnNewButton = new JButton("Projekt kopieren");
controlPanel.add(btnNewButton, "cell 1 2");
JButton button_3 = new JButton("Projekt archivieren");
controlPanel.add(button_3, "cell 1 3");
JButton button_4 = new JButton("Projekt importieren");
controlPanel.add(button_4, "cell 1 4");
mainFrame.getContentPane().add(statusLabel, "1, 2, fill, fill");
mainFrame.setVisible(true);
}
private void showMenuDemo(){
//create a menu bar
final JMenuBar menuBar = new JMenuBar();
//create menus
JMenu fileMenu = new JMenu("Projekt");
JMenu editMenu = new JMenu("Produkt");
final JMenu aboutMenu = new JMenu("Kriterien");
final JMenu linkMenu = new JMenu("Bewertung");
//create menu items
JMenuItem newMenuItem = new JMenuItem("Anlegen");
newMenuItem.setMnemonic(KeyEvent.VK_N);
newMenuItem.setActionCommand("New");
JMenuItem openMenuItem = new JMenuItem("Anzeigen");
openMenuItem.setActionCommand("Anzeigen");
JMenuItem saveMenuItem = new JMenuItem("Ändern");
saveMenuItem.setActionCommand("Ändern");
JMenuItem cutMenuItem = new JMenuItem("Löschen");
cutMenuItem.setActionCommand("Löschen");
JMenuItem copyMenuItem = new JMenuItem("Kopieren");
copyMenuItem.setActionCommand("Kopieren");
JMenuItem archivierenMenuItem = new JMenuItem("Archivieren");
archivierenMenuItem.setActionCommand("Archivieren");
JMenuItem importierenMenuItem = new JMenuItem("Importieren");
importierenMenuItem.setActionCommand("importieren");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.setActionCommand("Exit");
MenuItemListener menuItemListener = new MenuItemListener();
newMenuItem.addActionListener(menuItemListener);
openMenuItem.addActionListener(menuItemListener);
saveMenuItem.addActionListener(menuItemListener);
exitMenuItem.addActionListener(menuItemListener);
cutMenuItem.addActionListener(menuItemListener);
copyMenuItem.addActionListener(menuItemListener);
importierenMenuItem.addActionListener(menuItemListener);
final JCheckBoxMenuItem showWindowMenu = new JCheckBoxMenuItem("Show About", true);
showWindowMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(showWindowMenu.getState()){
menuBar.add(aboutMenu);
}else{
menuBar.remove(aboutMenu);
}
}
});
final JRadioButtonMenuItem showLinksMenu =
new JRadioButtonMenuItem("Show Links", true);
showLinksMenu.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(menuBar.getMenu(3)!= null){
menuBar.remove(linkMenu);
mainFrame.repaint();
}else{
menuBar.add(linkMenu);
mainFrame.repaint();
}
}
});
//add menu items to menus
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(showWindowMenu);
fileMenu.addSeparator();
fileMenu.add(showLinksMenu);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add( importierenMenuItem);
//add menu to menubar
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
menuBar.add(linkMenu);
//add menubar to the frame
mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
}
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand()
+ " JMenuItem clicked.");
}
}
}
请有人帮助我。我想创建这个数据库 GUI 布局,以便当我单击输入按钮时出现一个新行,但我不知道该怎么做。我是一名新的 Java 用户,我正在努力提高我的技能。
谢谢
【问题讨论】:
-
请附上您尝试过的代码。
标签: java swing user-interface layout-manager