Java 好看的label-textfield(-button)布局

class myBorderJPanel extends JPanel{

        private TitledBorder titledborder;

        private JPanel panel = new JPanel();

        myBorderJPanel(String title, GridLayout gridlayout){

                titledborder = BorderFactory.createTitledBorder(
                                BorderFactory.createEtchedBorder(
                                  EtchedBorder.LOWERED), title);
                titledborder.setTitleFont(new Font("Georgia",Font.BOLD,20));
                titledborder.setTitleJustification(TitledBorder.LEFT);
                titledborder.setTitlePosition(TitledBorder.DEFAULT_POSITION);

                setBorder(BorderFactory.createEmptyBorder(0,10,10,10));


                panel.setLayout(gridlayout);
                panel.setBorder(titledborder);
                add(panel);
        }
        void addC(Component c){
                c.setFont(new Font("Georgia",Font.BOLD,20));
                panel.add(c);
        }
}

使用:

                myBorderJPanel panel1 = new myBorderJPanel("rong",new GridLayout(3,2));
                panel1.addC(new JLabel("nx ="));
                panel1.addC(new JTextField(5));
                panel1.addC(new JLabel("nz ="));
                panel1.addC(new JTextField(5));
                panel1.addC(new JLabel("nz ="));
                panel1.addC(new JTextField(5));

效果截图:



以上截图代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.*;
class Demo{
        public static void main(String[]args){

                JFrame frame = new JFrame("BorderDemo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                myBorderJPanel panel1 = new myBorderJPanel("File");
                panel1.addC(new JTextField(15),0,0,5,1);
                panel1.addC(new JButton(new ImageIcon("picture/ButtonImg/folder16.png")),5,0,1,1);
                myBorderJPanel panel2 = new myBorderJPanel("");
                panel2.addC(new JLabel("nx"),0,0,1,1);
                panel2.addC(new JTextField(5),1,0,1,1);
                panel2.addC(new JLabel("nz"),0,1,1,1);
                panel2.addC(new JTextField(5),1,1,1,1);
                myBorderJPanel panel3 = new myBorderJPanel("");
                panel3.addC(new JLabel("dx"),0,0,1,2);
                panel3.addC(new JTextField(5),1,0,3,2);
                panel3.addC(new JLabel("dz"),0,2,1,2);
                panel3.addC(new JTextField(5),1,2,3,2);
                myBorderJPanel panel4 = new myBorderJPanel("++++");
                panel4.addC(panel2,0,0,1,1);
                panel4.addC(panel3,1,0,1,1);
                myBorderJPanel panel = new myBorderJPanel("");
                panel1.setAlignmentX(1f);
                panel.addC(panel1,0,0,1,1);
                panel.addC(panel4,0,1,1,1);
                frame.add(panel);
                frame.pack();
                frame.setVisible(true);
        }
}
class myBorderJPanel extends JPanel{
        private TitledBorder titledborder;
        private JPanel panel = new JPanel();
        private GridBagConstraints gc = new GridBagConstraints();
        private GridBagLayout gridbag = new GridBagLayout();
        myBorderJPanel(String title){
                titledborder = BorderFactory.createTitledBorder(
                                BorderFactory.createEtchedBorder(
                                  EtchedBorder.LOWERED), title);
                titledborder.setTitleFont(new Font("Georgia",Font.BOLD,20));
                titledborder.setTitleJustification(TitledBorder.LEFT);
                titledborder.setTitlePosition(TitledBorder.DEFAULT_POSITION);
                setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
                panel.setLayout(gridbag);
                panel.setBorder(titledborder);
                JPanel tmp = new JPanel();
                tmp.add(panel);
                add(tmp);
        }
        void addC(JComponent c, int gridx, int gridy,int gridwidth,int gridheight){
                gc.gridx = gridx; //x grid position
                gc.gridy = gridy; //y grid position
                gc.gridwidth = gridwidth;
                gc.gridheight = gridheight;
                gridbag.setConstraints(c, gc);
                c.setFont(new Font("Georgia",Font.BOLD,25));
                panel.add(c);
        }
}


以下是label-textfield(-button)布局

        private GridBagConstraints makeGbc(int ix, int iz) {
        / * Copyright: https://stackoverflow.com/questions/4699892/how-to-set-the-component-size-with-gridlayout-is-there-a-better-way * /
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = 1;
                gbc.gridheight = 1;
                gbc.gridx = ix;
                gbc.gridy = iz;
                gbc.weightx = ix;
                gbc.weighty = 1.0;
                gbc.insets = new Insets(5, 5, 5, 5);
                gbc.anchor = (ix == 0) ? GridBagConstraints.LINE_START  
                                       : GridBagConstraints.LINE_END;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                return gbc;
          }

使用方法:

                GridBagConstraints gbc = makeGbc(0, 0);
                parasPanel.add(labeltitle,gbc);
                JPanel p0 =new JPanel();
                p0.add(textfieldtitle);
                p0.add(buttontitle);
                p0.setBorder(BorderFactory.createEtchedBorder());
                gbc = makeGbc(1, 0);
                parasPanel.add(p0, gbc);

效果截图:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值