为什么我在 jmenubar 旁边有一个白色区域?
Why i'm getting a white area beside jmenubar?
我正在尝试创建一个包含两个 splitPanel 和一个菜单栏的对话框。两个 splitPanel 都包含 jlist.
但是当我 运行 我的代码时,我在菜单栏旁边得到了白色区域。在下图中,在 Organize 旁边创建了一个白色区域。
这是我的部分代码:
public class HistList extends JPanel {
JTable table = new JTable();
JMenuItem remove = new JMenuItem("Remove");
JScrollPane scrollPane = new JScrollPane();
MyTableModel model = new MyTableModel();
HistList() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
String[] title = {" ", "Time", "Name", "Location"};
Object[][] data = {
{false, "01:22:16", "Google", "http://www.google.com"},
{false, "01:22:16", "Yahoo - login", "https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a//mail.yahoo.com"},
{false, "01:22:29", "Oracle | Integrated Cloud Applications and Platform Services", "https://www.oracle.com/index.html"}
};
for (int i = 0; i < data.length; i++) {
model.addRow(data[i]);
}
this.table = new JTable(model);
this.table.setShowGrid(false);
this.scrollPane = new JScrollPane(this.table);
Dimension size = new Dimension(510, 380);
this.scrollPane.setPreferredSize(size);
this.scrollPane.getViewport().setBackground(Color.WHITE);
JTextField name = new JTextField();
name.setPreferredSize(new Dimension(scrollPane.getWidth(), 25));
JTextField locationText = new JTextField();
locationText.setPreferredSize(new Dimension(scrollPane.getWidth(), 25));
JPanel textPane = new JPanel();
JLabel nameLabel = new JLabel("Name:");
JPanel textPane1 = new JPanel();
textPane1.setLayout(new BoxLayout(textPane1, BoxLayout.X_AXIS));
textPane1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textPane1.add(Box.createHorizontalGlue());
textPane1.add(nameLabel);
textPane1.add(Box.createRigidArea(new Dimension(21, 0)));
textPane1.add(name);
JLabel locationLabel = new JLabel("Location:");
JPanel textPane2 = new JPanel();
textPane2.setLayout(new BoxLayout(textPane2, BoxLayout.X_AXIS));
textPane2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textPane2.add(Box.createHorizontalGlue());
textPane2.add(locationLabel);
textPane2.add(Box.createRigidArea(new Dimension(5, 0)));
textPane2.add(locationText);
textPane.setLayout(new BoxLayout(textPane, BoxLayout.Y_AXIS));
textPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
textPane.add(Box.createVerticalGlue());
textPane.add(textPane1);
textPane.add(Box.createRigidArea(new Dimension(0, 5)));
textPane.add(textPane2);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(scrollPane);
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
contentPane.add(Box.createVerticalGlue());
contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
contentPane.add(textPane);
JPanel pane = new JPanel(new BorderLayout());
pane.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0));
pane.setBackground(Color.WHITE);
String[] dates = {"12/15/2016", "12/30/2016"};
JList list;
list = new JList(dates);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JLabel label = new JLabel("Browsing Date");
pane.add(label, BorderLayout.NORTH);
pane.add(list, BorderLayout.WEST);
pane.setPreferredSize(new Dimension(150, 380));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane, contentPane);
JMenuBar organize = new JMenuBar();
organize.setOpaque(true);
organize.setBackground(getBackground());
JMenu menu = new JMenu("Organize");
menu.add(remove);
remove.setEnabled(false);
menu.setOpaque(true);
menu.setBackground(getBackground());
menu.setIcon(new ImageIcon(Hello.class.getResource("/rsz_todo-512.png")));
organize.add(menu);
JPanel finale = new JPanel(new BorderLayout());
finale.setOpaque(true);
finale.add(organize,BorderLayout.NORTH);
finale.add(splitPane,BorderLayout.CENTER);
setOpaque(true);
add(finale);
}
我不知道是什么造成了这个白色区域。我在这个部分很新。任何人都可以帮我解决它吗?请原谅我的愚蠢问题。提前致谢。
But when i run my code i get white area beside the menu bar
finale.add(organize,BorderLayout.NORTH);
不要将菜单栏添加到框架的内容窗格。
JFrame 为菜单栏保留了一个特殊位置。
阅读JFrameAPI找到合适的方法
阅读 Swing 教程中有关如何使用菜单的部分,了解更多信息和工作示例。
请注意 Swing 教程中的工作示例将帮助您更好地构建代码,以便在 Event Dispatch Thread (EDT)
上创建 GUI 组件。
代码也更多地采用 MCVE
的形式,尽管示例中的代码也过多。由于您的问题是关于菜单栏的,因此您只需要 JFrame、JMenuBar 和 JMenu 来演示您陈述的问题。
将本教程 link 放在手边,以获取所有 Swing 基础知识的示例。
我正在尝试创建一个包含两个 splitPanel 和一个菜单栏的对话框。两个 splitPanel 都包含 jlist.
但是当我 运行 我的代码时,我在菜单栏旁边得到了白色区域。在下图中,在 Organize 旁边创建了一个白色区域。
这是我的部分代码:
public class HistList extends JPanel {
JTable table = new JTable();
JMenuItem remove = new JMenuItem("Remove");
JScrollPane scrollPane = new JScrollPane();
MyTableModel model = new MyTableModel();
HistList() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
String[] title = {" ", "Time", "Name", "Location"};
Object[][] data = {
{false, "01:22:16", "Google", "http://www.google.com"},
{false, "01:22:16", "Yahoo - login", "https://login.yahoo.com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a//mail.yahoo.com"},
{false, "01:22:29", "Oracle | Integrated Cloud Applications and Platform Services", "https://www.oracle.com/index.html"}
};
for (int i = 0; i < data.length; i++) {
model.addRow(data[i]);
}
this.table = new JTable(model);
this.table.setShowGrid(false);
this.scrollPane = new JScrollPane(this.table);
Dimension size = new Dimension(510, 380);
this.scrollPane.setPreferredSize(size);
this.scrollPane.getViewport().setBackground(Color.WHITE);
JTextField name = new JTextField();
name.setPreferredSize(new Dimension(scrollPane.getWidth(), 25));
JTextField locationText = new JTextField();
locationText.setPreferredSize(new Dimension(scrollPane.getWidth(), 25));
JPanel textPane = new JPanel();
JLabel nameLabel = new JLabel("Name:");
JPanel textPane1 = new JPanel();
textPane1.setLayout(new BoxLayout(textPane1, BoxLayout.X_AXIS));
textPane1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textPane1.add(Box.createHorizontalGlue());
textPane1.add(nameLabel);
textPane1.add(Box.createRigidArea(new Dimension(21, 0)));
textPane1.add(name);
JLabel locationLabel = new JLabel("Location:");
JPanel textPane2 = new JPanel();
textPane2.setLayout(new BoxLayout(textPane2, BoxLayout.X_AXIS));
textPane2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textPane2.add(Box.createHorizontalGlue());
textPane2.add(locationLabel);
textPane2.add(Box.createRigidArea(new Dimension(5, 0)));
textPane2.add(locationText);
textPane.setLayout(new BoxLayout(textPane, BoxLayout.Y_AXIS));
textPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
textPane.add(Box.createVerticalGlue());
textPane.add(textPane1);
textPane.add(Box.createRigidArea(new Dimension(0, 5)));
textPane.add(textPane2);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
contentPane.add(scrollPane);
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
contentPane.add(Box.createVerticalGlue());
contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
contentPane.add(textPane);
JPanel pane = new JPanel(new BorderLayout());
pane.setBorder(BorderFactory.createEmptyBorder(15, 0, 0, 0));
pane.setBackground(Color.WHITE);
String[] dates = {"12/15/2016", "12/30/2016"};
JList list;
list = new JList(dates);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JLabel label = new JLabel("Browsing Date");
pane.add(label, BorderLayout.NORTH);
pane.add(list, BorderLayout.WEST);
pane.setPreferredSize(new Dimension(150, 380));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane, contentPane);
JMenuBar organize = new JMenuBar();
organize.setOpaque(true);
organize.setBackground(getBackground());
JMenu menu = new JMenu("Organize");
menu.add(remove);
remove.setEnabled(false);
menu.setOpaque(true);
menu.setBackground(getBackground());
menu.setIcon(new ImageIcon(Hello.class.getResource("/rsz_todo-512.png")));
organize.add(menu);
JPanel finale = new JPanel(new BorderLayout());
finale.setOpaque(true);
finale.add(organize,BorderLayout.NORTH);
finale.add(splitPane,BorderLayout.CENTER);
setOpaque(true);
add(finale);
}
我不知道是什么造成了这个白色区域。我在这个部分很新。任何人都可以帮我解决它吗?请原谅我的愚蠢问题。提前致谢。
But when i run my code i get white area beside the menu bar
finale.add(organize,BorderLayout.NORTH);
不要将菜单栏添加到框架的内容窗格。
JFrame 为菜单栏保留了一个特殊位置。
阅读JFrameAPI找到合适的方法
阅读 Swing 教程中有关如何使用菜单的部分,了解更多信息和工作示例。
请注意 Swing 教程中的工作示例将帮助您更好地构建代码,以便在 Event Dispatch Thread (EDT)
上创建 GUI 组件。
代码也更多地采用 MCVE
的形式,尽管示例中的代码也过多。由于您的问题是关于菜单栏的,因此您只需要 JFrame、JMenuBar 和 JMenu 来演示您陈述的问题。
将本教程 link 放在手边,以获取所有 Swing 基础知识的示例。