javaGridBagLayout佈局

import javax.swing.*;
import testGridBag.GBC;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Chuangkou extends JFrame{

	public static JTextArea daily = new JTextArea(19, 40);
	public JScrollPane jsp = new JScrollPane(getJtextArea(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	public JLabel broken = new JLabel("broken:");
	public JButton fileChoose = new JButton("上傳zip");
	public JTextField jtf = new JTextField(25);
	
	public JLabel goal = new JLabel("goal:");
	public JButton fileGoal = new JButton("上傳zip");
	public JTextField goalJtf = new JTextField(25);
	
	public JButton jstart = new JButton("開始");
	public JButton result = new JButton("結果");
	
	public JPanel j1 = new JPanel();
	public JPanel j2 = new JPanel();
	public JPanel j3 = new JPanel();
	public JCheckBox box1 = new JCheckBox("data");
	public JCheckBox box2 = new JCheckBox("status");
	public JCheckBox box3 = new JCheckBox("performance");
	public JCheckBox box4 = new JCheckBox("alarm");
	
	private JTextArea getJtextArea(){  
        if(daily == null){  
        	daily = new JTextArea();          
        }  
        daily.setLineWrap(true);
        return daily;  
    }
	public Chuangkou() {
		GridBagLayout layout = new GridBagLayout();
		GridBagConstraints s = new GridBagConstraints();;
		//s.fill=GridBagConstraints.BOTH;
		j1.setLayout(layout);
        layout.setConstraints(j1, new GBC(0, 0, 4, 1));
        this.add(j1);
        j2.setLayout(layout);
        layout.setConstraints(j2, new GBC(0, 1, 4, 1));
        this.add(j2);
        j3.setLayout(layout);
        layout.setConstraints(j3, new GBC(0, 4, 4, 1));
        this.add(j3);
        
		layout.setConstraints(box1, new GBC(0, 0, 1, 1));
		layout.setConstraints(box2, new GBC(1, 0, 1, 1));
		layout.setConstraints(box3, new GBC(2, 0, 1, 1));
		layout.setConstraints(box4, new GBC(3, 0, 1, 1));
		layout.setConstraints(broken, new GBC(0, 0, 1, 1));
		layout.setConstraints(jtf, new GBC(1, 0, 2, 1));
		layout.setConstraints(fileChoose, new GBC(3, 0, 1, 1));
		layout.setConstraints(goal, new GBC(0, 1, 1, 1));
		layout.setConstraints(goalJtf, new GBC(1, 1, 2, 1));
		layout.setConstraints(fileGoal, new GBC(3, 1, 1, 1));
		layout.setConstraints(jstart, new GBC(1, 2, 1, 1));
		layout.setConstraints(result, new GBC(2, 2, 1, 1));
		
		j2.add(broken);
		j2.add(jtf);
		j2.add(fileChoose);
		j2.add(goal);
		j2.add(goalJtf);
		j2.add(fileGoal);
		j2.add(jstart);
		j2.add(result);
		j3.add(jsp);
		
		j1.add(box1);
		j1.add(box2);
		j1.add(box3);
		j1.add(box4);
		this.setLayout(layout);
		this.setTitle("Hello World");
		//this.pack();
		this.setSize(500,500);
		this.setVisible(true);
		this.setBackground(Color.gray);
		//this.setResizable(false);
//		this.getContentPane().setVisible(true);
		//this.getContentPane().setBackground(Color.WHITE);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fileChoose.addActionListener(new MyActionListener());
		fileGoal.addActionListener(new GoalFileListener());
	}
	class GoalFileListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent arg0){
			JFileChooser fc = new JFileChooser("F:\\");
			int val = fc.showOpenDialog(null);
			if(val == fc.APPROVE_OPTION){
				goalJtf.setText(fc.getSelectedFile().toString());
			}else{
				goalJtf.setText("未選擇文件");
			}
		}
		
	}
	class MyActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            JFileChooser fc=new JFileChooser("F:\\");
            int val=fc.showOpenDialog(null);    //文件打開對話框
            if(val==fc.APPROVE_OPTION)
            {
                //正常選擇文件
                jtf.setText(fc.getSelectedFile().toString());
            }
            else
            {
                //未正常選擇文件,如選擇取消按鈕
                jtf.setText("未選擇文件");
            }
        }
    }
	public static void main(String[] args) {
		Chuangkou c = new Chuangkou();
	}
		
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章