当前位置: 代码迷 >> J2SE >> 关于Jpanel的显示和布局有关问题
  详细解决方案

关于Jpanel的显示和布局有关问题

热度:93   发布时间:2016-04-24 12:41:15.0
关于Jpanel的显示和布局问题
请看下面代码
Java code
 
package test;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Test extends JPanel implements ActionListener{

private static final long serialVersionUID = 1L;
private JRadioButton radio = null;
private JLabel message = null;
private String approval_id = null;

/**
* This is the default constructor
*/
public Test(String value) {
super();
initialize(value);
}

/**
* This method initializes this
*
* @return void
*/
private void initialize(String value) {
approval_id = value;
message = new JLabel();
message.setText(" <html>messagenfsfdfertertertertetertertertertsfsdfsdf <html>");
message.setBounds(50, 0, 200, 20);
this.setSize(331, 31);
radio = new JRadioButton();
radio.addActionListener(this);
radio.setBounds(0, 0, 20, 20);
this.setLayout(null);
this.add(radio);
this.add(message);
this.setSize(200,20);
}

/**
* This method initializes radio
*
* @return javax.swing.JRadioButton
*/
private JRadioButton getRadio() {
return radio;
}

public String getValue(){
return approval_id;
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new GridLayout(3,1));
ButtonGroup bgroup = new ButtonGroup();

Test t1 = new Test("11111111");
bgroup.add(t1.getRadio());
Test t2 = new Test("22222222");
bgroup.add(t2.getRadio());
Test t3 = new Test("333333333");
bgroup.add(t3.getRadio());
f.setSize(600,400);
f.add(t1);
f.add(t2);
f.add(t3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
System.out.println(getValue());
}
}

package test;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagWithContaints {

public static void main(String[] args) {
JFrame f = new JFrame(
"Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
JPanel p = new JPanel();

p.setLayout(new GridBagLayout());
// creates a constraints object
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 3, 3); // insets for all components
c.gridx = 0; // column 0
c.gridy = 0; // row 0
c.ipadx = 10; // increases components width by 10 pixels
c.ipady = 10; // increases components height by 10 pixels
p.add(new JButton("Java"), c); // constraints passed in
c.gridx = 1; // column 1
// c.gridy = 0; // comment out this for reusing the obj
c.ipadx = 0; // resets the pad to 0
c.ipady = 0;
p.add(new JButton("Source"), c);
c.gridx = 0; // column 0
c.gridy = 1; // row 1
p.add(new JButton("and"), c);
c.gridx = 1; // column 1
p.add(new JButton("sdfsfdsdf"), c);
c.gridy = 2;
c.gridx = 0;
[color=#FF0000]p.add(new Test("sdfsfdsdf"),c);[/color]