JPanel 设置为不可见,除默认值之外的组合框选择将其设置为可见,但缺少组件
我正在构建一个欧姆定律应用程序。请记住,这是我的第一个程序(没有教程),所以请友善 =) 这是运行它时的样子:
组合框有计算电压、计算欧姆等的选项...
中心面板设置为不可见,直到您从组合框中做出选择。当您进行选择时,它应该是这样的:
我面临的问题,是我第一次进行选择时,仅显示一个 JLabel/JTextArea 组合(每对位于垂直框内的自己的面板上):
如果我单击组合框并再次进行相同的选择,它将正确显示。我不明白为什么它在第一次点击时表现不正确。也许我在构建 GUI 时采取了错误的方法。这是到目前为止的代码:
private JFrame frame;
private String[] choiceList = {"", "Calculate Volts", "Calculate Amps", "Calculate Ohms", "Calculate Watts"};
private JTextField textField_2;
private JPanel centerPanel;
private String volts = "Volts";
private String amps = "Amps";
private String ohms = "Ohms";
private String watts = "Watts";
private JLabel var1Label;
private JLabel var2Label;
private JLabel var3Label;
private JFormattedTextField var1TextField;
private JFormattedTextField var2TextField;
private JFormattedTextField var3TextField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("BotsOne ElectriCalc");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel northPanel = new JPanel();
northPanel.setIgnoreRepaint(true);
northPanel.setBorder(new LineBorder(Color.GRAY));
frame.getContentPane().add(northPanel, BorderLayout.NORTH);
JLabel choiceLabel = new JLabel("Please make a selection:");
northPanel.add(choiceLabel);
JComboBox choiceCombo = new JComboBox(choiceList);
northPanel.add(choiceCombo);
choiceCombo.addActionListener(new ChoiceComboListener());
JPanel southPanel = new JPanel();
southPanel.setIgnoreRepaint(true);
southPanel.setBorder(new LineBorder(Color.GRAY));
frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
JLabel label = new JLabel("Answer:");
southPanel.add(label);
textField_2 = new JTextField();
textField_2.setColumns(10);
southPanel.add(textField_2);
centerPanel = new JPanel();
centerPanel.setVisible(false);
centerPanel.setBorder(new LineBorder(Color.GRAY));
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
Box centerPanelVertBox = Box.createVerticalBox();
centerPanelVertBox.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPanel.add(centerPanelVertBox);
centerPanelVertBox.setVisible(true);
centerPanelVertBox.setBorder(null);
JLabel pleaseEnterLabel = new JLabel("Please enter 2 of 3 values:");
pleaseEnterLabel.setBorder(new EmptyBorder(15, 0, 10, 0));
pleaseEnterLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPanelVertBox.add(pleaseEnterLabel);
JPanel var1Panel = new JPanel();
centerPanelVertBox.add(var1Panel);
var1Label = new JLabel("xxx");
var1Label.setAlignmentX(Component.CENTER_ALIGNMENT);
var1Panel.add(var1Label);
var1TextField = new JFormattedTextField(NumberFormat.getInstance());
var1TextField.setColumns(10);
var1Panel.add(var1TextField);
Panel var2Panel = new Panel();
centerPanelVertBox.add(var2Panel);
var2Label = new JLabel("xxx");
var2Label.setAlignmentX(Component.CENTER_ALIGNMENT);
var2Panel.add(var2Label);
var2TextField = new JFormattedTextField(NumberFormat.getInstance());
var2TextField.setColumns(10);
var2Panel.add(var2TextField);
Panel var3Panel = new Panel();
centerPanelVertBox.add(var3Panel);
var3Label = new JLabel("xxx");
var3Label.setAlignmentX(Component.CENTER_ALIGNMENT);
var3Panel.add(var3Label);
var3TextField = new JFormattedTextField(NumberFormat.getInstance());
var3TextField.setColumns(10);
var3Panel.add(var3TextField);
Panel calculatePanel = new Panel();
centerPanelVertBox.add(calculatePanel);
JButton calculateButton = new JButton("Calculate");
calculatePanel.add(calculateButton);
}
public class ChoiceComboListener implements ActionListener { //combobox actionlistener
public void actionPerformed(ActionEvent ev) {
JComboBox cb = (JComboBox)ev.getSource();
String currentComboSelection = (String)cb.getSelectedItem();
if (currentComboSelection.equals(choiceList[1])) { //choice 1 (Calculate Volts)
centerPanel.setVisible(true);
var1Label.setText(amps);
var2Label.setText(ohms);
var3Label.setText(watts);
//centerPanel.repaint();
}
if (currentComboSelection.equals(choiceList[2])) { //choice 2 (Calculate Amps)
centerPanel.setVisible(true);
var1Label.setText(volts);
var2Label.setText(ohms);
var3Label.setText(watts);
centerPanel.repaint();
}
if (currentComboSelection.equals(choiceList[3])) {
centerPanel.setVisible(true);
var1Label.setText(volts);
var2Label.setText(amps);
var3Label.setText(watts);
}
if (currentComboSelection.equals(choiceList[4])) {
centerPanel.setVisible(true);
var1Label.setText(volts);
var2Label.setText(amps);
var3Label.setText(ohms);
}
if (currentComboSelection.equals(choiceList[0])) {
centerPanel.setVisible(false);
//centerPanel.repaint();
}
}
}
如果你看一下底部,有一个名为 ChoiceComboListener 的内部类,这是组合框侦听器,我尝试过使用 repaint() 和各种其他东西,但我无法想出任何事物。任何帮助、指导或批评都是值得赞赏的。
I am building an ohms law app. Please keep in mind that is my first program ever (without tutorials) so please be kind =) Here's how it looks when you run it:
The combo box has options to calculate volts, calculate ohms, etc...
The center panel is set to invisible until you make a choice from the combobox. Here's what it should look like when you make a selection:
The problem I am facing, is that the first time I make a selection, only one of the JLabel/JTextArea combos (each pair is on its own panel inside a vertical box) are displayed:
If i click the comboBox and make the same selection again, it displays correctly. I can't figure out why it acts incorrectly on the first click. Perhaps I am taking the wrong approach in building the GUI. Here's the code so far:
private JFrame frame;
private String[] choiceList = {"", "Calculate Volts", "Calculate Amps", "Calculate Ohms", "Calculate Watts"};
private JTextField textField_2;
private JPanel centerPanel;
private String volts = "Volts";
private String amps = "Amps";
private String ohms = "Ohms";
private String watts = "Watts";
private JLabel var1Label;
private JLabel var2Label;
private JLabel var3Label;
private JFormattedTextField var1TextField;
private JFormattedTextField var2TextField;
private JFormattedTextField var3TextField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("BotsOne ElectriCalc");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel northPanel = new JPanel();
northPanel.setIgnoreRepaint(true);
northPanel.setBorder(new LineBorder(Color.GRAY));
frame.getContentPane().add(northPanel, BorderLayout.NORTH);
JLabel choiceLabel = new JLabel("Please make a selection:");
northPanel.add(choiceLabel);
JComboBox choiceCombo = new JComboBox(choiceList);
northPanel.add(choiceCombo);
choiceCombo.addActionListener(new ChoiceComboListener());
JPanel southPanel = new JPanel();
southPanel.setIgnoreRepaint(true);
southPanel.setBorder(new LineBorder(Color.GRAY));
frame.getContentPane().add(southPanel, BorderLayout.SOUTH);
JLabel label = new JLabel("Answer:");
southPanel.add(label);
textField_2 = new JTextField();
textField_2.setColumns(10);
southPanel.add(textField_2);
centerPanel = new JPanel();
centerPanel.setVisible(false);
centerPanel.setBorder(new LineBorder(Color.GRAY));
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
Box centerPanelVertBox = Box.createVerticalBox();
centerPanelVertBox.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPanel.add(centerPanelVertBox);
centerPanelVertBox.setVisible(true);
centerPanelVertBox.setBorder(null);
JLabel pleaseEnterLabel = new JLabel("Please enter 2 of 3 values:");
pleaseEnterLabel.setBorder(new EmptyBorder(15, 0, 10, 0));
pleaseEnterLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
centerPanelVertBox.add(pleaseEnterLabel);
JPanel var1Panel = new JPanel();
centerPanelVertBox.add(var1Panel);
var1Label = new JLabel("xxx");
var1Label.setAlignmentX(Component.CENTER_ALIGNMENT);
var1Panel.add(var1Label);
var1TextField = new JFormattedTextField(NumberFormat.getInstance());
var1TextField.setColumns(10);
var1Panel.add(var1TextField);
Panel var2Panel = new Panel();
centerPanelVertBox.add(var2Panel);
var2Label = new JLabel("xxx");
var2Label.setAlignmentX(Component.CENTER_ALIGNMENT);
var2Panel.add(var2Label);
var2TextField = new JFormattedTextField(NumberFormat.getInstance());
var2TextField.setColumns(10);
var2Panel.add(var2TextField);
Panel var3Panel = new Panel();
centerPanelVertBox.add(var3Panel);
var3Label = new JLabel("xxx");
var3Label.setAlignmentX(Component.CENTER_ALIGNMENT);
var3Panel.add(var3Label);
var3TextField = new JFormattedTextField(NumberFormat.getInstance());
var3TextField.setColumns(10);
var3Panel.add(var3TextField);
Panel calculatePanel = new Panel();
centerPanelVertBox.add(calculatePanel);
JButton calculateButton = new JButton("Calculate");
calculatePanel.add(calculateButton);
}
public class ChoiceComboListener implements ActionListener { //combobox actionlistener
public void actionPerformed(ActionEvent ev) {
JComboBox cb = (JComboBox)ev.getSource();
String currentComboSelection = (String)cb.getSelectedItem();
if (currentComboSelection.equals(choiceList[1])) { //choice 1 (Calculate Volts)
centerPanel.setVisible(true);
var1Label.setText(amps);
var2Label.setText(ohms);
var3Label.setText(watts);
//centerPanel.repaint();
}
if (currentComboSelection.equals(choiceList[2])) { //choice 2 (Calculate Amps)
centerPanel.setVisible(true);
var1Label.setText(volts);
var2Label.setText(ohms);
var3Label.setText(watts);
centerPanel.repaint();
}
if (currentComboSelection.equals(choiceList[3])) {
centerPanel.setVisible(true);
var1Label.setText(volts);
var2Label.setText(amps);
var3Label.setText(watts);
}
if (currentComboSelection.equals(choiceList[4])) {
centerPanel.setVisible(true);
var1Label.setText(volts);
var2Label.setText(amps);
var3Label.setText(ohms);
}
if (currentComboSelection.equals(choiceList[0])) {
centerPanel.setVisible(false);
//centerPanel.repaint();
}
}
}
If you look at the bottom, there is an inner class called ChoiceComboListener, this is the comboBox listener, I have tried playing around with repaint(), and various other things, but I can't come up with anything. Any help, guidance, or criticism is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个选择可能是使用 Card布局。本教程有一个工作示例,可以完全满足您的需求。
Another opton might be to use a Card Layout. The tutorial has a working example that does exactly what you want.
“请记住,这是我的第一个程序(没有教程)”——干得好,干得好。一个建议是,您可能希望考虑将电气单元放入枚举中,因为它们似乎是实践这一重要概念的完美场所。另外,您的 JTextField 可能会与 GridBagLayout 更好地对齐,但使用起来可能会非常。
例如,在枚举上:
那么用户的选择可以是一个枚举,这可以简化 gui 逻辑部分中的一些代码。
"Please keep in mind that is my first program ever (without tutorials)" -- Well done, well done. One suggestion is that you may wish to consider making your electrical units into an enum as they seem a perfect place to practice this important concept. Also, you JTextFields may line up better with a GridBagLayout, but it can be very persnickety to use.
e.g., on enum:
Then the users choice can be an enum, and this may simplify some of the code in your gui's logic portion.
我发现,我将第一个面板(正确显示的面板)声明为 JPanel,并将其他 2 个面板声明为常规面板。哎呀!无论如何,如果有人想提供一些建议或批评,我们仍然欢迎! – 机器人
I figured it out, I had the first panel (the one that was displaying correctly) declared as a JPanel, and I declared the other 2 as regular Panels. Sheesh! Anyways if anyone wants to supply some advice or criticism it's still welcomed! – Bots