想设置成5行1列的,可出来的是1行5列的!!这是为啥啊?
下面是全部代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public class SetFont extends JDialog implements ListSelectionListener{
public SetFont(){
win.setBounds(400,100,WIDTH,HEIGHT);
Container cont=win.getContentPane();
GridBagLayout grid=new GridBagLayout();
GridBagConstraints cons=new GridBagConstraints();
cons.gridwidth=GridBagConstraints.REMAINDER;
cons.fill=GridBagConstraints.HORIZONTAL;
//下面添加5个JPanel组件
grid.setConstraints(setFontList(),cons);
cont.add(setFontList());
grid.setConstraints(createFontShow(),cons);
cont.add(createFontShow());
grid.setConstraints(setFontSize(),cons);
cont.add(setFontSize());
grid.setConstraints(createFontBold(),cons);
cont.add(createFontBold());
grid.setConstraints(setButton(),cons);
cont.add(setButton());
cont.setLayout(grid);
win.setVisible(true);
}
private JScrollPane setFontList(){
fontList.addListSelectionListener(this);
JScrollPane scroll=new JScrollPane(fontList);
scroll.setSize(new Dimension(300,100));
return scroll;
}
public void valueChanged(ListSelectionEvent e)
{
fontTypeT=(String)fontList.getSelectedValue();
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));
}
private JPanel createFontShow(){
String text=new String("text 预览 show");
textShow=new JLabel(text,0);
//textShow.setPreferredSize(new Dimension(WIDTH,200));
textShow.setVerticalAlignment(1);
textShow.setFont(font);
jp=new JPanel();
jp.add(textShow);
jp.setBorder(BorderFactory.createBevelBorder(1,Color.white,Color.gray));
return jp;
}
private JPanel setFontSize(){
String[] sizes={Integer.toString(fontSizeT),"12","16","20","24","28","32","36","40","44"};
jc=new JComboBox(sizes);
//jc.setBounds(300,200,100,100);
jp=new JPanel();
jc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontSizeT=Integer.parseInt((String)jc.getSelectedItem());
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT)); }});
jp.add(jc);
return jp;
}
private JPanel createFontBold(){
jp=new JPanel();
JRadioButton boldB=new JRadioButton("加粗");
JRadioButton italicB=new JRadioButton("倾斜");
boldB.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
fontBoldT+=Font.BOLD;
else
fontBoldT-=Font.BOLD;
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));}});
italicB.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
fontBoldT+=Font.ITALIC;
else if(e.getStateChange()==ItemEvent.DESELECTED)
fontBoldT-=Font.ITALIC;
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));}});
jp.add(boldB);
jp.add(italicB);
return jp;
}
private JPanel setButton(){
JButton okB=new JButton("确定");
JButton exitB=new JButton("取消");
okB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontType=fontTypeT;fontSize=fontSizeT;
font=new Font(fontTypeT,fontBoldT,fontSizeT);
win.setVisible(false); }});
exitB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
font=new Font(fontType,fontBold,fontSize);
win.setVisible(false); }});
jp=new JPanel();
jp.add(okB);
jp.add(exitB);
jp.setBorder(BorderFactory.createLineBorder(Color.white));
return jp;
}
public static void main(String[] args){
new SetFont();
}
public Font getFont(){
return font;
}
public JFrame getWindow(){
return win;
}
//一次性变量,但需要声明为静态的
private static JComboBox jc;
private static JFrame win=new JFrame("在这里设置字体");
private static JLabel textShow;
private static int WIDTH=400,HEIGHT=500;
private static JPanel jp;
private static int a=0,b=1;
private String fontTypeT="微软雅黑",fontType=fontTypeT; //第1个为临时存储量,确定后将值传给fontType
private int fontBoldT=0,fontBold=fontBoldT;
private int fontSizeT=30,fontSize=fontSizeT; private Font font=new Font(fontTypeT,fontBoldT,fontSizeT);
private static GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
private static String[] fontNames=e.getAvailableFontFamilyNames();
private static JList fontList=new JList(fontNames);
}
下面是全部代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public class SetFont extends JDialog implements ListSelectionListener{
public SetFont(){
win.setBounds(400,100,WIDTH,HEIGHT);
Container cont=win.getContentPane();
GridBagLayout grid=new GridBagLayout();
GridBagConstraints cons=new GridBagConstraints();
cons.gridwidth=GridBagConstraints.REMAINDER;
cons.fill=GridBagConstraints.HORIZONTAL;
//下面添加5个JPanel组件
grid.setConstraints(setFontList(),cons);
cont.add(setFontList());
grid.setConstraints(createFontShow(),cons);
cont.add(createFontShow());
grid.setConstraints(setFontSize(),cons);
cont.add(setFontSize());
grid.setConstraints(createFontBold(),cons);
cont.add(createFontBold());
grid.setConstraints(setButton(),cons);
cont.add(setButton());
cont.setLayout(grid);
win.setVisible(true);
}
private JScrollPane setFontList(){
fontList.addListSelectionListener(this);
JScrollPane scroll=new JScrollPane(fontList);
scroll.setSize(new Dimension(300,100));
return scroll;
}
public void valueChanged(ListSelectionEvent e)
{
fontTypeT=(String)fontList.getSelectedValue();
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));
}
private JPanel createFontShow(){
String text=new String("text 预览 show");
textShow=new JLabel(text,0);
//textShow.setPreferredSize(new Dimension(WIDTH,200));
textShow.setVerticalAlignment(1);
textShow.setFont(font);
jp=new JPanel();
jp.add(textShow);
jp.setBorder(BorderFactory.createBevelBorder(1,Color.white,Color.gray));
return jp;
}
private JPanel setFontSize(){
String[] sizes={Integer.toString(fontSizeT),"12","16","20","24","28","32","36","40","44"};
jc=new JComboBox(sizes);
//jc.setBounds(300,200,100,100);
jp=new JPanel();
jc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontSizeT=Integer.parseInt((String)jc.getSelectedItem());
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT)); }});
jp.add(jc);
return jp;
}
private JPanel createFontBold(){
jp=new JPanel();
JRadioButton boldB=new JRadioButton("加粗");
JRadioButton italicB=new JRadioButton("倾斜");
boldB.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
fontBoldT+=Font.BOLD;
else
fontBoldT-=Font.BOLD;
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));}});
italicB.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
fontBoldT+=Font.ITALIC;
else if(e.getStateChange()==ItemEvent.DESELECTED)
fontBoldT-=Font.ITALIC;
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));}});
jp.add(boldB);
jp.add(italicB);
return jp;
}
private JPanel setButton(){
JButton okB=new JButton("确定");
JButton exitB=new JButton("取消");
okB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontType=fontTypeT;fontSize=fontSizeT;
font=new Font(fontTypeT,fontBoldT,fontSizeT);
win.setVisible(false); }});
exitB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
font=new Font(fontType,fontBold,fontSize);
win.setVisible(false); }});
jp=new JPanel();
jp.add(okB);
jp.add(exitB);
jp.setBorder(BorderFactory.createLineBorder(Color.white));
return jp;
}
public static void main(String[] args){
new SetFont();
}
public Font getFont(){
return font;
}
public JFrame getWindow(){
return win;
}
//一次性变量,但需要声明为静态的
private static JComboBox jc;
private static JFrame win=new JFrame("在这里设置字体");
private static JLabel textShow;
private static int WIDTH=400,HEIGHT=500;
private static JPanel jp;
private static int a=0,b=1;
private String fontTypeT="微软雅黑",fontType=fontTypeT; //第1个为临时存储量,确定后将值传给fontType
private int fontBoldT=0,fontBold=fontBoldT;
private int fontSizeT=30,fontSize=fontSizeT; private Font font=new Font(fontTypeT,fontBoldT,fontSizeT);
private static GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
private static String[] fontNames=e.getAvailableFontFamilyNames();
private static JList fontList=new JList(fontNames);
}
解决方案 »
- 大师请进
- 请教高人Communications link failure due to underlying exception:
- EXTJS问题请高手指点迷津
- java基于TCP的socket编程,客户端指定IP与服务器通信,为什么客户端却提示time out:conncet????
- 小谈VO”大谈”OO
- 一个算法的问题!我觉得很难!看哪位大侠能作出来!
- 请问怎样转换quoted-printable格式的字符串?
- struts的小问题!请大家帮忙!
- 请教weblogic服务启动时的问题
- 请问,JAVA里,怎样可以改变已定义数组的大小(类似于C语言中函数realloc的功能)。
- 看看我对 struts spring ORM框架 关系的理解对不对
- J2EE SSL 的问题
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;public class SetFont extends JDialog implements ListSelectionListener{
public SetFont(){
win.setBounds(400,100,WIDTH,HEIGHT);
Container cont=win.getContentPane();
GridBagLayout grid=new GridBagLayout();
cont.setLayout(grid);
GridBagConstraints cons=new GridBagConstraints();
// cons.gridwidth=GridBagConstraints.REMAINDER;
// cons.fill=GridBagConstraints.HORIZONTAL;
//下面添加5个JPanel组件
JScrollPane fontList=setFontList();
JPanel fontPanel=createFontShow();
JPanel fontSize=setFontSize();
JPanel fontBold=createFontBold();
JPanel btnPanel=setButton(); cons.gridwidth=1;
cons.gridheight=1;
cons.gridx=0;
cons.gridy=0;
grid.setConstraints(fontList,cons); cons.gridwidth=1;
cons.gridheight=1;
cons.gridx=0;
cons.gridy=1;
grid.setConstraints(fontPanel,cons); cons.gridwidth=1;
cons.gridheight=1;
cons.gridx=0;
cons.gridy=2;
grid.setConstraints(fontSize,cons); cons.gridwidth=1;
cons.gridheight=1;
cons.gridx=0;
cons.gridy=3;
grid.setConstraints(fontBold,cons); cons.gridwidth=1;
cons.gridheight=1;
cons.gridx=0;
cons.gridy=4;
grid.setConstraints(btnPanel,cons); cont.add(fontList); cont.add(fontPanel);
cont.add(fontSize);
cont.add(fontBold);
cont.add(btnPanel);
win.pack();
win.setVisible(true);
}
private JScrollPane setFontList(){
fontList.addListSelectionListener(this);
JScrollPane scroll=new JScrollPane(fontList);
scroll.setSize(new Dimension(300,100));
return scroll;
}
public void valueChanged(ListSelectionEvent e)
{
fontTypeT=(String)fontList.getSelectedValue();
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));
}
private JPanel createFontShow(){
String text=new String("text 预览 show");
textShow=new JLabel(text,0);
//textShow.setPreferredSize(new Dimension(WIDTH,200));
textShow.setVerticalAlignment(1);
textShow.setFont(font);
jp=new JPanel();
jp.add(textShow);
jp.setBorder(BorderFactory.createBevelBorder(1,Color.white,Color.gray));
return jp;
}
private JPanel setFontSize(){
String[] sizes={Integer.toString(fontSizeT),"12","16","20","24","28","32","36","40","44"};
jc=new JComboBox(sizes);
//jc.setBounds(300,200,100,100);
jp=new JPanel();
jc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontSizeT=Integer.parseInt((String)jc.getSelectedItem());
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT)); }});
jp.add(jc);
return jp;
}
private JPanel createFontBold(){
jp=new JPanel();
JRadioButton boldB=new JRadioButton("加粗");
JRadioButton italicB=new JRadioButton("倾斜");
boldB.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
fontBoldT+=Font.BOLD;
else
fontBoldT-=Font.BOLD;
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));}});
italicB.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange()==ItemEvent.SELECTED)
fontBoldT+=Font.ITALIC;
else if(e.getStateChange()==ItemEvent.DESELECTED)
fontBoldT-=Font.ITALIC;
textShow.setFont(new Font(fontTypeT,fontBoldT,fontSizeT));}});
jp.add(boldB);
jp.add(italicB);
return jp;
}
private JPanel setButton(){
JButton okB=new JButton("确定");
JButton exitB=new JButton("取消");
okB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontType=fontTypeT;fontSize=fontSizeT;
font=new Font(fontTypeT,fontBoldT,fontSizeT);
win.setVisible(false);
}
});
exitB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
font=new Font(fontType,fontBold,fontSize);
win.setVisible(false);
}
});
jp=new JPanel();
jp.add(okB);
jp.add(exitB);
jp.setBorder(BorderFactory.createLineBorder(Color.white));
return jp;
}
public static void main(String[] args){
new SetFont();
}
public Font getFont(){
return font;
}
public JFrame getWindow(){
return win;
} //一次性变量,但需要声明为静态的
private static JComboBox jc;
private static JFrame win=new JFrame("在这里设置字体");
private static JLabel textShow;
private static int WIDTH=400,HEIGHT=500;
private static JPanel jp;
private static int a=0,b=1; private String fontTypeT="微软雅黑",fontType=fontTypeT; //第1个为临时存储量,确定后将值传给fontType
private int fontBoldT=0,fontBold=fontBoldT;
private int fontSizeT=30,fontSize=fontSizeT; private Font font=new Font(fontTypeT,fontBoldT,fontSizeT);
private static GraphicsEnvironment e=GraphicsEnvironment.getLocalGraphicsEnvironment();
private static String[] fontNames=e.getAvailableFontFamilyNames();
private static JList fontList=new JList(fontNames);
}