想设置成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);
}

解决方案 »

  1.   

    我不是特别清楚为什么楼主的代码是横向输出的,改了几个地方不是很确定哪个起作用了,其实你只是简单的纵向输出用GridLayout就可以了,这里给贴了用GridBagLayout实现的,能够五行一列的情况(个人觉得一行五列或许更符合用户习惯)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();
            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);
    }