我今天注意到我的程序非常奇怪的行为。基本上我有 JToolBar uder,它是带有 JTable 的 JScrollPane。两者都在 JFrame 内的 JPanel 内。每个容器都使用 MigLayout。
现在,如果我启动应用程序,这是它的默认外观:
但是,如果我移动 JToolBar 并将其剪辑回原来的位置,现在它看起来像这样:
突然之间没有边界了。如果一开始根本没有任何东西,我会更喜欢,但是改变 GUI 的外观根本不是一个好的功能......如果你知道什么是错的,请帮助:)
代码:
public class Gui extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel mainPnl = null;
private JToolBar toolbar = null;
private Session session = null;
public Gui(Session session) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException e) {
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
this.session = session;
setTitle("PRO2-Contact Manager v_0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800,420);
setResizable(true);
initMenu();
initMainPnl();
initToolbar();
initTable();
// KeyboardFocusManager manager =
// KeyboardFocusManager.getCurrentKeyboardFocusManager();
// manager.addKeyEventDispatcher(new MyDispatcher(aList));
setLocationRelativeTo(null);
setVisible(true);
}
private void initMenu() {
JMenuBar menu = new JMenuBar();
MenuListener ml = new MenuListener();
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
menu.add(file);
JMenuItem exit = new JMenuItem("Exit");
exit.setMnemonic(KeyEvent.VK_E);
exit.addActionListener(ml);
file.add(exit);
JMenu help = new JMenu("Help");
help.setMnemonic(KeyEvent.VK_H);
menu.add(help);
JMenuItem controls = new JMenuItem("Controls");
controls.setMnemonic(KeyEvent.VK_C);
controls.addActionListener(ml);
help.add(controls);
JMenuItem about = new JMenuItem("About");
about.setMnemonic(KeyEvent.VK_A);
about.addActionListener(ml);
help.add(about);
setJMenuBar(menu);
}
private void initMainPnl(){
mainPnl = new JPanel(new MigLayout());
add(mainPnl);
}
private void initToolbar() {
toolbar = new JToolBar(JToolBar.HORIZONTAL);
toolbar.add(new JButton());
mainPnl.add(toolbar,"wrap");
}
private void initTable() {
MyTable table = new MyTable(new MyTableModel(this));
JScrollPane sp = new JScrollPane(table);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
mainPnl.add(sp,"w 100%, h 100%");
}}