麻烦有人看下我的程序,为什么3个JLIST的窗口跑到右边去了,我的想法是想让他们在JTextField下面的import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;public class GridBagWindow extends JFrame {
private JTextArea text;
private JButton okButton,cancelButton;
private JList fontName,fontSize,fontStyle;
private JTextField copy1,copy2,copy3;
GraphicsEnvironment g;//定义系统字体对象
String[] size = {"8","10","12","14","16","18","20","22","24","26","28","32","36","48","72","96"};
String[] style= {"PLAIN","BOLD","ITALIC"};
public GridBagWindow() {
Container font = getContentPane();
JLabel label1=new JLabel("Font:");
JLabel label2=new JLabel("Style:");
JLabel label3=new JLabel("Size:");
g = GraphicsEnvironment.getLocalGraphicsEnvironment();//获取系统字体
String[] fontname = g.getAvailableFontFamilyNames();
fontName = new JList(fontname);
fontName.setVisibleRowCount(5);
fontName.setFixedCellWidth(170);
copy1= new JTextField(1);
copy1.setColumns(17);
fontName.addListSelectionListener(new Handler());
fontStyle = new JList(style);
fontStyle.setVisibleRowCount(5);
fontStyle.setFixedCellWidth(80);
copy2= new JTextField(1);
copy2.setColumns(10);
fontStyle.addListSelectionListener(new Handler());
fontSize = new JList(size);
fontSize.setVisibleRowCount(5);
fontSize.setFixedCellWidth(80);
copy3= new JTextField(1);
copy3.setColumns(10);
fontSize.addListSelectionListener(new Handler());
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
GridBagLayout layout = new GridBagLayout();
font.setLayout(layout);
GridBagConstraints n = new GridBagConstraints();
n.fill =GridBagConstraints.VERTICAL;
n.gridx = 0;
n.gridy = 0;
layout.setConstraints(label1,n);
font.add(label1);
n.gridx = 2;
n.gridy = 0;
layout.setConstraints(label2,n);
font.add(label2);
n.gridx = 3;
n.gridy = 0;
layout.setConstraints(label3,n);
font.add(label3);
n.gridx = 0;
n.gridy = 1;
n.gridwidth = 2;
layout.setConstraints(copy1,n);
font.add(copy1);
n.gridx = 2;
n.gridy = 1;
n.gridwidth = 1;
layout.setConstraints(copy2,n);
font.add(copy2);
n.gridx = 3;
n.gridy = 1;
n.gridwidth = 1;
layout.setConstraints(copy3,n);
font.add(copy3);
n.gridx = 4;
n.gridy = 1;
layout.setConstraints(okButton,n);
font.add(okButton);
n.gridx = 0;
n.gridy = 2;
n.gridwidth = 2;
layout.setConstraints(fontName,n);
font.add(new JScrollPane(fontName));
n.gridx = 2;
n.gridy = 2;
layout.setConstraints(fontStyle,n);
font.add(new JScrollPane(fontStyle));
n.gridx = 3;
n.gridy = 2;
layout.setConstraints(fontSize,n);
font.add(new JScrollPane(fontSize));
n.gridx = 4;
n.gridy = 2;
layout.setConstraints(cancelButton,n);
font.add(cancelButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.setTitle("GridBagWindow");
window.pack();
window.setVisible(true);
}
private class Handler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
copy1.setText((String)fontName.getSelectedValue());
copy2.setText((String)fontStyle.getSelectedValue());
copy3.setText((String)fontSize.getSelectedValue());
}
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;public class GridBagWindow extends JFrame {
private JTextArea text;
private JButton okButton,cancelButton;
private JList fontName,fontSize,fontStyle;
private JTextField copy1,copy2,copy3;
GraphicsEnvironment g;//定义系统字体对象
String[] size = {"8","10","12","14","16","18","20","22","24","26","28","32","36","48","72","96"};
String[] style= {"PLAIN","BOLD","ITALIC"};
public GridBagWindow() {
Container font = getContentPane();
JLabel label1=new JLabel("Font:");
JLabel label2=new JLabel("Style:");
JLabel label3=new JLabel("Size:");
g = GraphicsEnvironment.getLocalGraphicsEnvironment();//获取系统字体
String[] fontname = g.getAvailableFontFamilyNames();
fontName = new JList(fontname);
fontName.setVisibleRowCount(5);
fontName.setFixedCellWidth(170);
copy1= new JTextField(1);
copy1.setColumns(17);
fontName.addListSelectionListener(new Handler());
fontStyle = new JList(style);
fontStyle.setVisibleRowCount(5);
fontStyle.setFixedCellWidth(80);
copy2= new JTextField(1);
copy2.setColumns(10);
fontStyle.addListSelectionListener(new Handler());
fontSize = new JList(size);
fontSize.setVisibleRowCount(5);
fontSize.setFixedCellWidth(80);
copy3= new JTextField(1);
copy3.setColumns(10);
fontSize.addListSelectionListener(new Handler());
okButton = new JButton("OK");
cancelButton = new JButton("Cancel");
GridBagLayout layout = new GridBagLayout();
font.setLayout(layout);
GridBagConstraints n = new GridBagConstraints();
n.fill =GridBagConstraints.VERTICAL;
n.gridx = 0;
n.gridy = 0;
layout.setConstraints(label1,n);
font.add(label1);
n.gridx = 2;
n.gridy = 0;
layout.setConstraints(label2,n);
font.add(label2);
n.gridx = 3;
n.gridy = 0;
layout.setConstraints(label3,n);
font.add(label3);
n.gridx = 0;
n.gridy = 1;
n.gridwidth = 2;
layout.setConstraints(copy1,n);
font.add(copy1);
n.gridx = 2;
n.gridy = 1;
n.gridwidth = 1;
layout.setConstraints(copy2,n);
font.add(copy2);
n.gridx = 3;
n.gridy = 1;
n.gridwidth = 1;
layout.setConstraints(copy3,n);
font.add(copy3);
n.gridx = 4;
n.gridy = 1;
layout.setConstraints(okButton,n);
font.add(okButton);
n.gridx = 0;
n.gridy = 2;
n.gridwidth = 2;
layout.setConstraints(fontName,n);
font.add(new JScrollPane(fontName));
n.gridx = 2;
n.gridy = 2;
layout.setConstraints(fontStyle,n);
font.add(new JScrollPane(fontStyle));
n.gridx = 3;
n.gridy = 2;
layout.setConstraints(fontSize,n);
font.add(new JScrollPane(fontSize));
n.gridx = 4;
n.gridy = 2;
layout.setConstraints(cancelButton,n);
font.add(cancelButton);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.setTitle("GridBagWindow");
window.pack();
window.setVisible(true);
}
private class Handler implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
copy1.setText((String)fontName.getSelectedValue());
copy2.setText((String)fontStyle.getSelectedValue());
copy3.setText((String)fontSize.getSelectedValue());
}
}
}
font.add(控件名,约束名);
如 font.add(cancelButton,n);