怎样将两个JPanel/JButton 放在 BorderLayout 的北部区域?

huangapple go评论81阅读模式
英文:

How do I put two Jpanels/Jbuttons in the borderlayout north section?

问题

如何在边界布局中的“North”区域显示两个JPanels?

这里有一个示例代码,它输出一个具有三个不同行(Top、Middle、Bottom)的GUI。第一行有一个按钮,第二行有3个按钮,第三行有一个按钮。

package borderLayoutDemo;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;

public class BorderLayoutDemo {
    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame fj = new JFrame("Demonstration of Border Layout");
        fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton jbtn1 = new JButton("UP");
        JButton jbtn2 = new JButton("DOWN");
        JButton jbtn3 = new JButton("LEFT");
        JButton jbtn4 = new JButton("RIGHT");
        JButton jbtn5 = new JButton("MIDDLE");
        JPanel pnl = new JPanel();
        pnl.setLayout(new BorderLayout());
        pnl.add(jbtn1, BorderLayout.NORTH);
        pnl.add(jbtn2, BorderLayout.SOUTH);
        pnl.add(jbtn3, BorderLayout.WEST);
        pnl.add(jbtn4, BorderLayout.EAST);
        pnl.add(jbtn5, BorderLayout.CENTER);
        fj.add(pnl);
        fj.pack();
        fj.setVisible(true);
    }
}

上述代码的输出为:
以上代码的输出

然而,我希望在“North”部分有两个JPanels,就像这样形成4个“行”:

|---------------button--------------| //north
|---------------button2-------------| //north
----------------center---------------  //center
|---------------button3-------------|  //south

我尝试过简单地按照以下方式添加:

pnl.add(jbtn1, BorderLayout.NORTH);
pnl.add(jbtn2, BorderLayout.NORTH);

但这里发生的情况是第二个按钮只替换了第一个按钮:

|---------------button2-------------| //north
----------------center---------------  //center
|---------------button3-------------|  //south

如何在北部布局区域获取两行?

英文:

How do I display two JPanels in the 'North' in borderlayout?

Here's and example code that outputs a GUI with three distinct rows, Top, Middle, Bottom. There's one button covering the first row, 3 buttons covering the second row, and one covering the bottom row.

package borderLayoutDemo;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;

public class BorderLayoutDemo {
    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame fj = new JFrame("Demonstration of Border Layout");
        fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton jbtn1 = new JButton("UP");
        JButton jbtn2 = new JButton("DOWN");
        JButton jbtn3 = new JButton("LEFT");
        JButton jbtn4 = new JButton("RIGHT");
        JButton jbtn5 = new JButton("MIDDLE");
        JPanel pnl = new JPanel();
        pnl.setLayout(new BorderLayout());
        pnl.add(jbtn1, BorderLayout.NORTH);
        pnl.add(jbtn2, BorderLayout.SOUTH);
        pnl.add(jbtn3, BorderLayout.WEST);
        pnl.add(jbtn4, BorderLayout.EAST);
        pnl.add(jbtn5, BorderLayout.CENTER);
        fj.add(pnl);
        fj.pack();
        fj.setVisible(true);
    }
}

Output of above code:
output of above code

However, I'd like there to be two jpanels in the North section so it'd make 4 "rows" like this:

|---------------button--------------| //north
|---------------button2-------------| //north
----------------center---------------  //center
|---------------button3-------------|  //south

I've tried simply just adding it as follows:

pnl.add(jbtn1, BorderLayout.NORTH);
pnl.add(jbtn2, BorderLayout.NORTH);

But what happens here is the second button just replaces the first one:

|---------------button2-------------| //north
----------------center---------------  //center
|---------------button3-------------|  //south

How would I get two rows in the north layout area?

答案1

得分: 2

创建一个更复杂的GUI很简单,当你将GUI视为一个带有许多必要的JPanelsJFrame时。

这是你要寻找的GUI。

我为JFrame的每个部分(NORTH、CENTER和SOUTH)创建了一个JPanel。这些JPanels中的每一个都使用了BorderLayout,这样当你扩展GUI时,NORTH和SOUTH按钮的高度保持不变。

这是完整的可运行示例代码。

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class BorderLayoutDemo implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new BorderLayoutDemo());
    }

    @Override
    public void run() {
        JFrame fj = new JFrame("演示Border布局");
        fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        fj.add(createNorthPanel(), BorderLayout.NORTH);
        fj.add(createCenterPanel(), BorderLayout.CENTER);
        fj.add(createSouthPanel(), BorderLayout.SOUTH);

        fj.pack();
        fj.setLocationByPlatform(true);
        fj.setVisible(true);
    }

    private JPanel createNorthPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        JButton button1 = new JButton("北按钮");
        panel.add(button1, BorderLayout.NORTH);

        JButton button2 = new JButton("北按钮2");
        panel.add(button2, BorderLayout.SOUTH);

        return panel;
    }

    private JPanel createCenterPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        JButton button = new JButton("中心按钮");
        panel.add(button, BorderLayout.CENTER);

        return panel;
    }

    private JPanel createSouthPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        JButton button = new JButton("南按钮");
        panel.add(button, BorderLayout.SOUTH);

        return panel;
    }
}
英文:

Creating a more complex GUI is straightforward when you think of a GUI as a JFrame with as many JPanels as necessary to define the GUI.

Here's the GUI you were looking for.

怎样将两个JPanel/JButton 放在 BorderLayout 的北部区域?

I created a JPanel for each section of the JFrame (NORTH, CENTER, and SOUTH). Each of those JPanels used a BorderLayout so that when you expand the GUI, the NORTH and SOUTH buttons stay the same height.

Here's the complete runnable example code.

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BorderLayoutDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new BorderLayoutDemo());
}
@Override
public void run() {
JFrame fj = new JFrame("Demonstration of Border Layout");
fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fj.add(createNorthPanel(), BorderLayout.NORTH);
fj.add(createCenterPanel(), BorderLayout.CENTER);
fj.add(createSouthPanel(), BorderLayout.SOUTH);
fj.pack();
fj.setLocationByPlatform(true);
fj.setVisible(true);
}
private JPanel createNorthPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button1 = new JButton("North Button");
panel.add(button1, BorderLayout.NORTH);
JButton button2 = new JButton("North Button 2");
panel.add(button2, BorderLayout.SOUTH);
return panel;
}
private JPanel createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Center Button");
panel.add(button, BorderLayout.CENTER);
return panel;
}
private JPanel createSouthPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("South Button");
panel.add(button, BorderLayout.SOUTH);
return panel;
}
}

huangapple
  • 本文由 发表于 2020年10月19日 22:54:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64429905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定