在flowLayout java中组织JButton
我正在努力做一个好看的布局 我试图在左侧添加一个按钮,在右侧添加两个按钮。 我尝试了很多方法来让它好看,所以效果不好。任何帮助都会很好在flowLayout java中组织JButton,java,layout,Java,Layout,我正在努力做一个好看的布局 我试图在左侧添加一个按钮,在右侧添加两个按钮。 我尝试了很多方法来让它好看,所以效果不好。任何帮助都会很好 JPanel btn1 = new JPanel(); btn1.setLayout(new FlowLayout(FlowLayout.RIGHT)); btn1.add(new JButton("btn1")); JButton btn2 = new JButton("btn2"); btn1.add(btn2); pane.add(btn1, Border
JPanel btn1 = new JPanel();
btn1.setLayout(new FlowLayout(FlowLayout.RIGHT));
btn1.add(new JButton("btn1"));
JButton btn2 = new JButton("btn2");
btn1.add(btn2);
pane.add(btn1, BorderLayout.SOUTH);
由于不允许我发布图像:
-----------------------------------------
| ------------------------------------- |
| | | |
| | | |
| | | |
| | | |
| ------------------------------------- |
| [ enter ] [btn1][btn2] |
-----------------------------------------
我如何将“回车”按钮设置到左侧
谢谢
尝试3:
JPanel southPanel = new JPanel(new GridLayout(1,2));
JPanel enter = new JPanel();
JPanel btn1_btn2 = new JPanel();
enter.add(new JButton("Enter"));
enter.setLayout((new FlowLayout(FlowLayout.LEADING)));
southPanel.add(enter);
btn1_btn2.add(new JButton("btn1"));
btn1_btn2.add(new JButton("btn2"));
btn1_btn2.setLayout((new FlowLayout(FlowLayout.TRAILING)));
southPanel.add(btn1_btn2);
可以嵌套具有不同FlowLayout方向(前导、尾随)的面板
以下是您要求的示例: 尽管GridBagLayout可能非常复杂,但它非常灵活。一旦你熟悉了你的选择(重量、锚定、ipadx等),这其实也没那么难 我强烈建议您阅读GridBagLayout,并了解它附带的功能。 与其他布局不同,GridBagLayout依赖于另一个对象(调用约束)为组件设置属性。所有组件通常共享相同的GridBagConstraints,以避免不必要的对象关联
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class App {
private static JFrame frame;
public static void main(String[] args) {
initFrame();
}
private static void initFrame() {
try {
SwingUtilities.invokeAndWait(new Runnable() { //all swing actions should be handled on EDT
public void run() {
frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close process on X
addComponents(frame.getContentPane()); //add components to frame
frame.setLocationRelativeTo(null); //set frame in center of screen
frame.setVisible(true);
}
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
}
private static void addComponents(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JButton button = new JButton("button1");
JButton button2 = new JButton("b2");
JButton button3 = new JButton("b3");
//we are anchroing our button to the bottom-left side of the screen
gbc.anchor = GridBagConstraints.SOUTHWEST;
//weightx and y specifiy that our comp isn't just mindlessly floating around
//for weightx, if there is any extra space on the X axis, fill ass needed
//same for weighty. if we didnt have these vars, our comp will float in the middle
//basically, if you dont have these, your "anchor" wont be noticed
gbc.weighty = .5;
gbc.weightx = .5;
pane.add(button, gbc);
//since we are now working with the other 2 buttons, make it south east
//we must change our weightx back to 0, to ensure there will be no spacing between
//our two buttons (even if there is extra space, dont use it)
gbc.anchor = GridBagConstraints.SOUTHEAST;
gbc.weightx = 0;
pane.add(button2, gbc);
pane.add(button3, gbc);
}
}
您有很多选项可以考虑,其中之一是将[n]、[bTN1]和[bTN2]放入具有网格布局的面板中,CyL1有[进入] CyL2空Cy3空单元格4包含[BTN1]和[BTN2]的面板,如果您不介意使用不同的布局,则应该检查GRIDWORD或GRIDBAG布局。由于GridBagLayout的灵活性,我更喜欢它。如果你想要一份工作,请告诉我example@VinceEmigh如果你能举个例子就好了。“我不熟悉这个设计,愿意到处玩玩。”MehdiKaramosly我不太了解你。很抱歉,我仍然无法创建它。请看一下我的帖子中的尝试2:不,对于南面板,您要做的是设置布局
GridLayout(1,2)
。然后您需要向该面板添加两个面板。左侧为FlowLayout.LEADING,右侧为FlowLayout.Training。在左侧面板中,添加enter按钮。在右边的面板上,你添加了另外两个按钮。谢谢你,它工作正常。请检查我的帖子中的尝试3。我有一个问题,我不知道btn1和btn2如何与事件侦听器一起工作,因为变量名是相同的。我将如何与btn1和btn2交互?你知道我的意思吗?类似于btn1表示开始计时器,或btn2表示停止计时器。