gpt4 book ai didi

java - 如何在Jframe中分离Jpanel

转载 作者:行者123 更新时间:2023-12-01 23:04:43 24 4
gpt4 key购买 nike

我正在尝试实现这种布局

enter image description here

我不知道为什么我会得到这个

enter image description here

这是我的代码

package testing;

import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;

public class Testing
{


public static class GridPanel extends JPanel
{
public GridPanel()
{
setLayout(new GridLayout(4,4));
setBackground(Color.GREEN);
this.setPreferredSize(new Dimension(500,100));



JButton b1 = new JButton ("Button 1");
JButton b2 = new JButton ("Button 2");
JButton b3 = new JButton ("Button 3");
JButton b4 = new JButton ("Button 4");
JButton b5 = new JButton ("Button 5");
JButton b6 = new JButton ("Button 6");
JButton b7 = new JButton ("Button 7");
JButton b8 = new JButton ("Button 8");
JButton b9 = new JButton ("Button 9");
JButton b10 = new JButton ("Button 10");
JButton b11 = new JButton ("Button 11");
JButton b12 = new JButton ("Button 12");
JButton b13 = new JButton ("Button 13");
JButton b14 = new JButton ("Button 14");
JButton b15 = new JButton ("Button 15");
JButton b16 = new JButton ("Button 16");

add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);

}

}



public static void main(String[] args)
{
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));



JPanel jp1 = new JPanel();
jp1.setPreferredSize(new Dimension(500,100));
JTextField jt1 = new JTextField(8);
jp1.add(jt1);


GridPanel gp = new GridPanel();

primary.add(jp1);
primary.add(gp);

JFrame jf = new JFrame();
jf.setPreferredSize(new Dimension(500,500));
jf.add(primary);
jf.pack();
jf.setVisible(true);

}


}

我不知道如何分离框架中的两个面板,我哪里做错了??

最佳答案

尝试这样的事情:

public static void main(String[] args) 
{

JPanel primary = new JPanel(new BorderLayout());
primary.setOpaque(true);

JTextField jt1 = new JTextField(8);
jt1.setPreferredSize(new Dimension(0, 30));
primary.add(jt1, BorderLayout.NORTH);


GridPanel gp = new GridPanel();
primary.add(gp, BorderLayout.CENTER);

JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setPreferredSize(new Dimension(400,200));
jf.setContentPane(primary);
jf.pack();
jf.setVisible(true);

}

关于java - 如何在Jframe中分离Jpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22984729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com