Java JComboBox 不显示
Java JComboBox not displaying
Java 的新手。无法显示下拉框
我最初在按钮之前有代码,但是当我 运行 代码(intelliJ idea)时,下拉菜单和按钮都没有出现。
Whosebug 需要更多详细信息。不知道要添加什么。我确信这只是语法中的一个菜鸟错误。
package com.example.guiTest;
import javax.swing.*;
import java.awt.*;
import java.lang.Object;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JComboBox;
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame("Position");
window.setVisible(true);
window.setSize(500, 500);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(dim.width/2-window.getSize().width/2, dim.height/2-window.getSize().height/2);;
JPanel panel = new JPanel();
panel.setLayout(null);
window.add(panel);
JLabel test = new JLabel("This is a test");
test.setBounds(20,0,120,120);
panel.add(test);
JButton button = new JButton("Compile");
button.setBounds(250, 250, 120, 50);
panel.add(button);
String[] noteArray = {"a", "b"};
JComboBox note = new JComboBox(noteArray);
note.setPreferredSize(new Dimension(200,130));
note.setLocation(new Point(200,200));
note.setEditable(true);
note.setSelectedIndex(3);
note.setVisible(true);
panel.add(note);
}
}
您在面板 JPanel 上使用 null
布局,快速回答是空布局要求您完全指定所有添加组件的大小和位置,即 size 不是 preferredSize。你设置的是后者,而不是前者。
更好的答案是不使用 null
布局和 setBounds(...)
。虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但是您创建的 Swing GUI 越多,您在使用它们时 运行 就会遇到更严重的困难。它们不会在 GUI 调整大小时调整您的组件的大小,它们是皇家 来增强或维护的,它们在放置在滚动窗格中时完全失败,它们在所有平台或屏幕分辨率上查看时看起来 gawd-awful和原来的不一样。
编辑:
或者您最终将 JComboBox 放在 JButton 的正上方(正如您的代码所做的那样!)。
编辑:
另一个问题:在将所有组件添加到 GUI 之前,不要在 JFrame 上调用 setVisible(true)
。
编辑:
然而 另一个 问题:note.setSelectedIndex(3);
注释组合框只有 2 个项目,为什么要将选定索引设置为 3?这肯定会失败。
例如:
import java.awt.BorderLayout;
import javax.swing.*;
public class BetterMain extends JPanel {
private static final String[] ITEMS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private static final int ROWS = 25;
private static final int COLUMNS = 40;
private JButton button = new JButton("Button");
private JTextField textField = new JTextField(COLUMNS / 2);
private JComboBox<String> myCombo = new JComboBox<>(ITEMS);
private JTextArea textArea = new JTextArea(ROWS, COLUMNS);
public BetterMain() {
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("This is a JLabel"));
topPanel.add(myCombo);
topPanel.add(textField);
topPanel.add(button);
JScrollPane scrollPane = new JScrollPane(textArea);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(topPanel, BorderLayout.PAGE_START);
}
private static void createAndShowGui() {
BetterMain mainPanel = new BetterMain();
JFrame frame = new JFrame("BetterMain");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Java 的新手。无法显示下拉框
我最初在按钮之前有代码,但是当我 运行 代码(intelliJ idea)时,下拉菜单和按钮都没有出现。
Whosebug 需要更多详细信息。不知道要添加什么。我确信这只是语法中的一个菜鸟错误。
package com.example.guiTest;
import javax.swing.*;
import java.awt.*;
import java.lang.Object;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JComboBox;
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame("Position");
window.setVisible(true);
window.setSize(500, 500);
window.setResizable(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation(dim.width/2-window.getSize().width/2, dim.height/2-window.getSize().height/2);;
JPanel panel = new JPanel();
panel.setLayout(null);
window.add(panel);
JLabel test = new JLabel("This is a test");
test.setBounds(20,0,120,120);
panel.add(test);
JButton button = new JButton("Compile");
button.setBounds(250, 250, 120, 50);
panel.add(button);
String[] noteArray = {"a", "b"};
JComboBox note = new JComboBox(noteArray);
note.setPreferredSize(new Dimension(200,130));
note.setLocation(new Point(200,200));
note.setEditable(true);
note.setSelectedIndex(3);
note.setVisible(true);
panel.add(note);
}
}
您在面板 JPanel 上使用 null
布局,快速回答是空布局要求您完全指定所有添加组件的大小和位置,即 size 不是 preferredSize。你设置的是后者,而不是前者。
更好的答案是不使用 null
布局和 setBounds(...)
。虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但是您创建的 Swing GUI 越多,您在使用它们时 运行 就会遇到更严重的困难。它们不会在 GUI 调整大小时调整您的组件的大小,它们是皇家 来增强或维护的,它们在放置在滚动窗格中时完全失败,它们在所有平台或屏幕分辨率上查看时看起来 gawd-awful和原来的不一样。
编辑:
或者您最终将 JComboBox 放在 JButton 的正上方(正如您的代码所做的那样!)。
编辑:
另一个问题:在将所有组件添加到 GUI 之前,不要在 JFrame 上调用 setVisible(true)
。
编辑:
然而 另一个 问题:note.setSelectedIndex(3);
注释组合框只有 2 个项目,为什么要将选定索引设置为 3?这肯定会失败。
例如:
import java.awt.BorderLayout;
import javax.swing.*;
public class BetterMain extends JPanel {
private static final String[] ITEMS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private static final int ROWS = 25;
private static final int COLUMNS = 40;
private JButton button = new JButton("Button");
private JTextField textField = new JTextField(COLUMNS / 2);
private JComboBox<String> myCombo = new JComboBox<>(ITEMS);
private JTextArea textArea = new JTextArea(ROWS, COLUMNS);
public BetterMain() {
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("This is a JLabel"));
topPanel.add(myCombo);
topPanel.add(textField);
topPanel.add(button);
JScrollPane scrollPane = new JScrollPane(textArea);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(topPanel, BorderLayout.PAGE_START);
}
private static void createAndShowGui() {
BetterMain mainPanel = new BetterMain();
JFrame frame = new JFrame("BetterMain");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}