我制作了一个Java swing应用程序。我的主要课程完成整个SwingUtilities.invokeLater(new Runnable()
的工作。
我的第二堂课,我用过的所有东西:
JPanel container = (JPanel) getContentPane();
然后通过调用..添加所有位。
container.add([name of component]
我现在想把整个'应用'变成JSplitPane
。因此,我希望我的应用程序在一方,而另一方则在右侧。
我该怎么做?
public class one{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new App("main");
f.setSize(1920,1080);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public class App extends JFrame {
SuppressWarnings("empty-statement")
public App(String title) {
super(title);
JPanel container = (JPanel) getContentPane();
container.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton new button = new JButton("new");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
container.add(new, c);
JButton next to button = new JButton("next to");
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
container.add(new, c);
//I HAVE LOTS OF SWING COMPONENTS AND BUTTONS about 30
}
我想在分割窗格的左侧显示所有这些内容吗?
我该怎么做?
答案 0 :(得分:5)
JFrame
延伸,而不是添加任何功能,而是将所有应用程序组件和逻辑移至单独的JPanel
JSplitPane
的实例,向其添加“主”面板,向其添加辅助窗格JFrame
的实例,将拆分窗格添加到其中...... <强>已更新强>
public class TestSplitPane {
public static void main(String[] args) {
new TestSplitPane();
}
public TestSplitPane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
pane.setLeftComponent(new MainPane());
pane.setRightComponent(new JLabel("On the right"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MainPane extends JPanel {
public MainPane() {
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton newButton = new JButton("new");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
add(newButton, c);
JButton next = new JButton("next to");
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 2;
//ENTER LOTS OF CONSTRAINTS
add(next, c);
}
}
}