面板java中的静态按钮定位

时间:2017-01-25 21:23:40

标签: java

我有这个代码但是只要我展开或调整框架大小,按钮就会移动到中心位置。我试图添加简单的面板和按钮,我需要两个相同尺寸的面板垂直/水平分割,每个面板应该有左侧的按钮和文本区域,以便在按钮被单击到相应的面板后有一些文本输出。

package jobmonitor;

import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class PanelTry {

     static JTextArea jText = new JTextArea("");
     static JTextArea jText1= new JTextArea("");

  public static void main(String args[]) {
    JFrame f = new JFrame();
    GridLayout myLayout = new GridLayout(2,1); 
    f.setLayout(myLayout);
    JButton button1;
    JButton button2;

    JPanel p1 = new JPanel();
    p1.setBorder ( new TitledBorder ( new EtchedBorder (), "" ) );
    button1 = new JButton("Server1");
    p1.setLayout(null);
    button1.setBounds(0, 5, 100, 20);
    p1.add(button1);
    f.add(p1);

    JPanel p2 = new JPanel();
    p2.setBorder ( new TitledBorder ( new EtchedBorder (), "" ) );
    button2 = new JButton("Server2");
    p2.setLayout(null);
    button2.setBounds(0, 20, 100, 20);
    p2.add(button2);
    f.add(p2);

    jText.setBounds(new Rectangle(100,20,500, 500));
    jText.setSize(800,300);
    jText.setEditable(false);

    JScrollPane scroll = new JScrollPane (jText);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    p1.add(scroll);

    jText1.setBounds(new Rectangle(100, 20,200, 200));
    jText1.setSize(800,300);
    jText1.setEditable(false);
    p1.add(jText);
    p2.add(jText1);


    // setLayout(null);
    f.setDefaultCloseOperation(3);
    f.setSize(700, 700);
    f.setVisible(true);



  }
}

0 个答案:

没有答案