我到了某个地方,但现在我遇到了一个更大的障碍:我似乎无法理解如何创建一个监听器,将我的JLabel上的信息更改为存储在我的JTextArea中的输入和输出信息。
这是我的代码,可以给你一个想法:
public class DisplayStudent extends JFrame {
private JMenuItem jmiConnect, jmiClose, jmiExit, jmiSearch, jmiClear;
private JPanel contentPane;
private JPanel contentPane2;
private JTextArea info;
private String information;
public JPanel contentPane() {
JPanel something = new JPanel(new BorderLayout());
//something.setLayout();
String information;
info = new JTextArea(4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel();
one.setBackground(Color.WHITE);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setSize(10, 10);
two.add(new JLabel(" No Connection "), BorderLayout.WEST);
two.add(new JLabel(" Number of Students = "), BorderLayout.EAST);
something.add(two);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("Display Student");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
DisplayStudent bar = new DisplayStudent();
frame.setJMenuBar(bar.DisplayStudent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
//frame.setJMenuBar(jmb);
}
public JMenuBar DisplayStudent() {
//Drop down menu
JMenuBar jmb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
jmb.add(fileMenu);
JMenu editMenu = new JMenu("Edit");
editMenu.setMnemonic('E');
jmb.add(editMenu);
//Buttons in menu - "File" menu
JMenuItem jmiConnect = new JMenuItem("Connect", 'C');
JMenuItem jmiClose = new JMenuItem("Close", 'L');
JMenuItem jmiExit = new JMenuItem("Exit", 'E');
fileMenu.add(jmiConnect);
fileMenu.add(jmiClose);
fileMenu.add(jmiExit);
//Buttons in menu - "Edit" menu
JMenuItem jmiSearch = new JMenuItem("Search", 'S');
JMenuItem jmiClear = new JMenuItem("Clear", 'B');
editMenu.add(jmiSearch);
editMenu.add(jmiClear);
jmiConnect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String database_name;
System.out.println("Please enter your database.");
Scanner input = new Scanner(System.in);
database_name = input.next();
}
});
jmiClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit?", "Question!", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Toodleloo!");
}
}
});
jmiExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jmiSearch.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
jmiClear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
return jmb;
}
public static void main(String[] args) {
GUI();
}
}
我很抱歉继续咨询。阅读不足以理解这一点。我再次感谢大家的帮助。
好的,当我回去时,我遇到了一个非常大的错误。这是:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at displaystudent.DisplayStudent$1.actionPerformed(DisplayStudent.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:157)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
如果我问得太多,我很抱歉。我现在已经知道了。
答案 0 :(得分:1)
“将名称转移到JLabel,将其从”No Connection“更改为”Connected to database_name“
问题:您的JLabel
没有对象引用。你这样做
two.add(new JLabel(" No Connection "), BorderLayout.WEST);
使难以(并非不可能)访问JLabel
。而是给它一个像
JLabel statusLabel = new JLabel(" No Connection ");
public JPanel contentPane() {
two.add(statusLabel, BorderLayout.WEST);
然后你可以从JMenuItem
的听众那里访问它。
jmiConnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
...
statusLabel.setText(...);
}
});
这是另一个建议,不要试图将控制台与GUI混合,即Scanner(System.in)
。您可能希望使用类似JOptionPane.showInputDialog(..)
甚至JCheckBoxMenuItem
的内容,这是菜单中的复选框,您可以检查数据库名称。然后,只需将结果附加到侦听器的文本区域即可。
<强>更新强>
为什么要创建一个新的DisplayStudent
来放入框架?由于该方法返回JPanel
,只需执行此操作
JPanel panel = contentPane();
frame.setContentPane(panel);
不要让您的课程延长JFrame
。您已经使用JFrame
的实例作为顶级容器。摆脱extends JFrame
请勿调用返回与您的班级JMenuBar
同名的DisplayStudent
的方法。称之为createMenuBar()
,然后执行此操作
JMenuMenuBar menuBar = createMenuBar();
frame.setJMenuBar(menuBar);
摆脱DisplayStudent .. = new DisplayStudent();