Java JSplitPane-将应用程序一分为二?
我制作了一个JavaSwing应用程序。我的主类完成了整个Java JSplitPane-将应用程序一分为二?,java,swing,jpanel,jsplitpane,Java,Swing,Jpanel,Jsplitpane,我制作了一个JavaSwing应用程序。我的主类完成了整个SwingUtilities.invokeLater(newrunnable()工作 我的第二节课,我用过所有东西: JPanel container = (JPanel) getContentPane(); 然后通过调用..添加所有位 container.add([name of component] 我现在想把整个“应用程序”放到一个JSplitPane中。因此,我希望我的应用程序在一边,而其他东西在右边 我该怎么做 pu
SwingUtilities.invokeLater(newrunnable()
工作
我的第二节课,我用过所有东西:
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
}
我希望所有这些都在拆分窗格的左侧
我该怎么做
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
}
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);
}
}
}
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);
}
}
}
一种方法是将内容放入两个容器(例如,
JPanel
)并将其添加到拆分窗格中,然后将拆分窗格添加到内容窗格中。要更快地获得更好的帮助,请发布当前代码的详细信息(而不是代码片段和描述)。这样更好吗?我刚刚编写了一些模拟代码来演示我的观点。我看不出有什么理由创建one
类只是为了将main方法放入其中。“one”类用于多线程应用程序……我想我也看到了。一种方法是将内容放入两个容器中(例如JPanel
)并将它们添加到拆分窗格中,然后将拆分窗格添加到内容窗格中。要更快获得更好的帮助,请发布当前代码的摘要(而不是代码片段和说明)。这样更好吗?我刚刚编写了一些模拟代码来演示我的观点。我看不出你为什么要创建one
类只是为了把main方法放在里面。one类是用来对应用程序进行多线程处理的……我想我也看到了。你有没有可能为我键入一个快速的SSCCE?“我只是将这些方法应用到我的代码中,它成功了!”这就改变了,我的意思是,它当然做到了;)任何理由为什么,当我在中间向左边分度时,它不会移动和调整所有的摆动部件。如果我调整整个窗口的大小,它会受到面板的优先选择/ Min SsieWHELL的限制,但是它不适合在窗格中缩放。真的不确定吗?=三一重工有机会给我们输入一个SSCSCE吗?我刚刚将这些方法应用到我的代码中,它成功了!“这是一个改变,我的意思是,当然,它确实是这样的)=为什么,当我在中间向左边分度时,它不会改变和调整所有的摆动部件。如果我调整整个窗口的大小,它会受到面板的优先选择/ Min SsieWHELL的限制,但是它不适合在窗格中缩放。