簡體   English   中英

在JFrame中放置和調整JPanel的大小

[英]Placing and resizing JPanel in JFrame

我想要一個帶有JPanelJFrame ,看起來像這樣。

樣本布局

左側的2個面板寬度為60%,高度為50%,然后右側的面板寬度為40%,高度為100%我如何實現這一目標?

  import javax.swing.*;
  import java.awt.*;
   public class INV{

private JFrame mainFrame;
private JPanel Outer_panel1;
private JPanel Outer_panel2;
private JPanel Panel1;
private JPanel Panel2;
private JPanel Panel3;
private JPanel Panel4;
private JButton b1;
private JButton b2;
private JButton b3;
private JComboBox List;
private JComboBox List2;
private JTextField Item_name;
private JTextField Dec_qty;
private JTextField Inc_qty;
private static String[] Items = {"wood","steel"};
private JTable Table;
private JScrollPane Scroll;
static double xsize;
static double ysize;

    public INV(){
        gui();
    }

    public void gui(){
        mainFrame = new JFrame("sample");
        GridBagConstraints Con = new GridBagConstraints();

        //Left Panel
        Dec_qty=new JTextField(3); 
        Inc_qty=new JTextField(3); 
        List = new JComboBox(Items);
        List2 = new JComboBox(Items);
        b1 = new JButton("OK");
        b2 = new JButton("OK");


        Outer_panel1 = new JPanel(new GridBagLayout());
        Outer_panel1.setBackground(Color.yellow);

        Panel1 = new JPanel();
        Panel1.setBackground(Color.blue);
        Panel2 = new JPanel();
        Panel2.setBackground(Color.red);



        Outer_panel1.add(b1);
        Outer_panel1.add(b2);

        Panel1.add(List);
        Panel2.add(List2);
        Panel1.add(Inc_qty);
        Panel2.add(Dec_qty);
        Panel1.add(b1);
        Panel2.add(b2);

        Con.gridx = 0;
        Con.gridy = 0;
        Con.ipady = 300;
        Con.fill = GridBagConstraints.VERTICAL;
        Outer_panel1.add(Panel1,Con);
        Con.gridx = 0;
        Con.gridy = 1;
        Con.ipady = 0;
        Con.weighty = 1.0;
        Outer_panel1.add(Panel2,Con);


        mainFrame.add(Outer_panel1, BorderLayout.LINE_START);


        //right panel
        Outer_panel2 = new JPanel(new GridBagLayout());
        Outer_panel2.setBackground(Color.CYAN);

        Panel3 = new JPanel();
        Panel3.setBackground(Color.MAGENTA);
        Panel4 = new JPanel();
        Panel4.setBackground(Color.ORANGE);

        //Panel3
        Item_name =new JTextField(20); 
        b3 = new JButton("Search");

        //Panel4 and Table
        String[] columnNames = {"column1","column2"};
        Object[][] data = {{"sample1","sample2"},
                            {"sample3","sample4"}};

        Table = new JTable(data, columnNames);
        Table.setPreferredScrollableViewportSize(new Dimension(200,200));
        Table.setFillsViewportHeight(true);
        Scroll = new JScrollPane(Table);

        Panel3.add(Item_name);
        Panel3.add(b3);
        Panel4.add(Scroll);

        Con.gridx = 0;
        Con.gridy = 0;
        Con.ipady = 0;
        Con.weighty = 0;
        Outer_panel2.add(Panel3, Con);
        Con.gridx = 0;
        Con.gridy = 1;
        Con.ipady = 0;  
        Con.fill = GridBagConstraints.BOTH;
        Con.weighty = 1.0;

        Outer_panel2.add(Panel4, Con);
        mainFrame.add(Outer_panel2, BorderLayout.EAST);

        mainFrame.setVisible(true);
        mainFrame.setSize(900, 500);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


public static void main(String[] args){
    new INV();
}
}

你永遠不應該手動控制大小。 如果用戶想調整大小怎么辦? 你必須做無用的計算,而java會很樂意使用布局管理器為你做。

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

JFrame frame = new JFrame();
frame.setLayout(null);
frame.setSize(600, 400);

JPanel panel1 = new JPanel();
panel1.setBounds(0, 0, 400, 200);
panel1.setBackground(Color.YELLOW);
panel1.setLayout(null);

JPanel panel2 = new JPanel();
panel2.setBounds(400, 0, 200, 400);
panel2.setBackground(Color.RED);
panel2.setLayout(null);

JPanel panel3 = new JPanel();
panel3.setBounds(0, 0, 600, 400);
panel3.setBackground(Color.BLACK);
panel3.setLayout(null);

panel3.add(panel1);
panel3.add(panel2);
frame.add(panel3);

//YOU CAN USE  LAYOUT = NULL TO MANUALLY POSITION YOUR PANELS
//IT IS BEST TO USE LAYOUT MANAGERS, BUT SETTING THE POSITIONS MANUALLY IS THE EASIEST
//SPECIALLY IF YOU WILL JUST .setResizable(false)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM