请大家帮我必要code对齐

请大家帮我必要code对齐

本文介绍了为什么我的组件出去的界限,请大家帮我必要code对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 进口net.java.dev.designgridlayout.Tag;
  进口net.java.dev.designgridlayout.DesignGridLayout;
  进口javax.swing.table.DefaultTableModel中;
  进口的javax.swing *。
  进口javax.swing.event中的*。
  进口java.awt中*。
  导入java.awt.event中*。

  类alignmentprob
  {
  JFrame的JF;
  容器C,C5;
  JDesktopPane的JDP;
  JInternalFrame的JIF5;
  JLabel的i5l1,i5l2,i5l3,i5l4,i5l5,i5l6,i5l7;
  JTextField的i5t1,i5t2,i5t3;
  的JComboBox<字符串> i5cb1;
  JButton的i5b1,i5b2,i5b3;
  JSeparator对象i5sep1,i5sep2,i5sep3,i5sep4,i5sep5,i5sep6;
  JTable的i5Table1;
  DefaultTableModel方面i5Model;
  的String [] i5Credit = {A,B};
  的String [] i5ColumnNames = {姓名,数量,价格/公斤,速度/ DZN,总AMT。};
  JScrollPane的i5t1sp1;

  公共alignmentprob()
  {
  JF =新的JFrame();
  JDP =新的JDesktopPane();
  JF.setVisible(真正的);
  JF.setSize(800×600);
  JIF5 =新的JInternalFrame(每日分析,真的,真的,真的,真的);
  C = JF.getContentPane();
  C.add(JDP,BorderLayout.CENTER);
  JIF5.setVisible(真正的);
  JIF5.setBounds(10,10,600,500);
  C5 = JIF5.getContentPane();
  DesignGridLayout layout5 =新DesignGridLayout(C5);

i5l1 =新的JLabel(运费详细信息);
i5l2 =新的JLabel(日期:);

i5l3 =新的JLabel(销售详细信息);
i5l4 =新的JLabel(现金销售:卢比。);
i5l5 =新的JLabel(信贷);

i5l6 =新的JLabel(开支);
i5l7 =新的JLabel(食品与茶叶:卢比。);


i5t1 =新的JTextField(20);
i5t2 =新的JTextField(20);
i5t3 =新的JTextField(20);

i5cb1 =新的JComboBox<字符串>(i5Credit);

i5b1 =新的JButton(保存);
i5b2 =新的JButton(复位);
i5b3 =新的JButton(关闭);

i5sep1 =新的JSeparator();
i5sep2 =新的JSeparator();
i5sep3 =新的JSeparator();
i5sep4 =新的JSeparator();
i5sep5 =新的JSeparator();
i5sep6 =新的JSeparator();

i5Model =新的DefaultTableModel(i5ColumnNames,5);
i5Table1 =新的JTable(i5Model){@覆盖
公共布尔isCellEditable(INT为arg0,INT ARG1)
{
返回false;
}
       };


i5t1sp1 =新JScrollPane中(i5Table1);


。layout5.row()左()加(i5sep1).fill伪()withOwnRowWidth()。
。layout5.row()中心()加(i5l1)。
。layout5.row()左()加(i5sep2).fill伪()withOwnRowWidth()。
。layout5.row()格(i5l2)。新增(i5t1);

。layout5.row()左()加(i5sep3).fill伪()withOwnRowWidth()。
。layout5.row()中心()加(i5l3)。
。layout5.row()左()加(i5sep4).fill伪()withOwnRowWidth()。
。layout5.row()格(i5l4)。新增(i5t2);
。layout5.row()格(i5l5)。新增(i5cb1);
。layout5.row()左()加(i5t1sp1);

。layout5.row()左()加(i5sep5).fill伪()withOwnRowWidth()。
。layout5.row()中心()加(i5l6)。
。layout5.row()左()加(i5sep6).fill伪()withOwnRowWidth()。
。layout5.row()格(i5l7)。新增(i5t3);
。layout5.row()中心()加(i5b1)。新增(i5b2)。新增(i5b3);

JDP.add(JIF5);
}

公共静态无效的主要(字符串的args [])
{
 新alignmentprob();
}
}
 

我的问题是,该文本框是太长,出界。我做了这个表有5行,但它往往占用更多的空间。我其实是想调整运费详细资料和并行销售明细,使用分离的垂直线。请帮我code做的这一切,让自己的形式看起来整洁,是完全可见。

解决方案

我是不是能够重现基于code您已经张贴的问题。也许有别的东西,我没有看到导致出现此问题。

即使使用第三方的布局管理器,如 DesignGridLayout 你可以按照使用嵌套布局方法的JP​​anel 的组货运细节和销售的细节部分。

关于垂直分割,不认为这是可能的DesignGridLayout。虽然你可以有组件跨越多行不与的JSeparator 工作

计算器不是code厂因此在大多数情况下,人们不会做你的工作给你,但他们可以提供有用的例子。但正如我已经全部复制到你的code测试它,我已经使用嵌套布局的方式做了一些改变,我在这里做一个例外。希望这会有所帮助:

 进口javax.swing.JButton中;
进口javax.swing.JComboBox中;
进口javax.swing.JFrame中;
进口javax.swing.JInternalFrame中;
进口javax.swing.JLabel中;
进口javax.swing.JPanel中;
进口javax.swing.JScrollPane中;
进口javax.swing.JSeparator中;
进口javax.swing.JTable中;
进口javax.swing.JTextField中;
进口javax.swing.SwingUtilities中;
进口javax.swing.table.DefaultTableModel中;
进口net.java.dev.designgridlayout.DesignGridLayout;

公共类演示{

    私人无效createAndShowGUI(){

        JLabel的i5l1 =新的JLabel(运费详细信息);
        JLabel的i5l2 =新的JLabel(日期:);
        JLabel的i5l3 =新的JLabel(车号:);
        JLabel的i5l4 =新的JLabel(来源:);
        JLabel的i5l5 =新的JLabel(项目);
        JLabel的i5l6 =新的JLabel(数量);
        JLabel的i5l7 =新的JLabel(千克。);
        JLabel的i5l8 =新的JLabel(价格:卢比。);
        JLabel的i5l15 =新的JLabel(销售详细信息);
        JLabel的i5l16 =新的JLabel(现金销售:卢比。);
        JLabel的i5l17 =新的JLabel(信用:卢比。);
        JLabel的i5l18 =新的JLabel(开支);
        JLabel的i5l19 =新的JLabel(食品与茶:RS);
        JLabel的i5l20 =新的JLabel(工资待遇:卢比。);
        JLabel的i5l21 =新的JLabel(杂项费用:卢比。);

        JTextField的i5t1 =新的JTextField(20);
        JTextField的i5t2 =新的JTextField(20);
        JTextField的i5t3 =新的JTextField(20);
        JTextField的i5t4 =新的JTextField(20);
        JTextField的i5t11 =新的JTextField(20);
        JTextField的i5t12 =新的JTextField(20);
        JTextField的i5t13 =新的JTextField(20);
        JTextField的i5t14 =新的JTextField(20);

        JComboBox中i5cb1 =新的JComboBox<>();
        JComboBox中i5cb2 =新的JComboBox<>();
        JComboBox中i5cb3 =新的JComboBox<>();

        JButton的i5b1 =新的JButton(保存);
        JButton的i5b2 =新的JButton(复位);
        JButton的i5b3 =新的JButton(关闭);

        JSeparator对象i5sep1 =新的JSeparator();
        JSeparator对象i5sep2 =新的JSeparator();
        JSeparator对象i5sep3 =新的JSeparator();
        JSeparator对象i5sep4 =新的JSeparator();
        JSeparator对象i5sep5 =新的JSeparator();
        JSeparator对象i5sep6 =新的JSeparator();

        [对象] columnNames =新的对象[] {列#1,列#2,列#3,列#4};
        的DefaultTableModel模型=新的DefaultTableModel(columnNames,10);
        JTable的表=新的JTable(模型);

        JScrollPane的i5t1sp1 =新JScrollPane中(表);

        JPanel的freightPanel =新的JPanel();
        DesignGridLayout布局1 =新DesignGridLayout(freightPanel);

        。layout1.row()左()加(i5sep1).fill伪()withOwnRowWidth()。
        。layout1.row()中心()加(i5l1)。
        。layout1.row()左()加(i5sep2).fill伪()withOwnRowWidth()。
        。layout1.row()格(i5l2)。新增(i5t1);
        。layout1.row()格(i5l3)。新增(i5t2);
        。layout1.row()格(i5l4)。新增(i5cb1);
        。layout1.row()格(i5l5)。新增(i5cb2);
        。layout1.row()格(i5l6)。新增(i5t3)。新增(i5l7);
        。layout1.row()格(i5l8)。新增(i5t4);

        。layout1.row()左()加(i5sep5).fill伪()withOwnRowWidth()。
        。layout1.row()中心()加(i5l18)。
        。layout1.row()左()加(i5sep6).fill伪()withOwnRowWidth()。
        。layout1.row()格(i5l19)。新增(i5t12);
        。layout1.row()格(i5l20)。新增(i5t13);
        。layout1.row()格(i5l21)。新增(i5t14);

        JPanel的salePanel =新的JPanel();
        DesignGridLayout布局2 =新DesignGridLayout(salePanel);

        。layout2.row()左()加(i5sep3).fill伪()withOwnRowWidth()。
        。layout2.row()中心()加(i5l15)。
        。layout2.row()左()加(i5sep4).fill伪()withOwnRowWidth()。
        。layout2.row()格(i5l16)。新增(i5t11);
        。layout2.row()格(i5l17)。新增(i5cb3);
        。layout2.row()格()加(i5t1sp1);

        JInternalFrame的internalFrame =新的JInternalFrame(每日分析,真的,真的,真的,真的);

        DesignGridLayout mainLayout =新DesignGridLayout(internalFrame.getContentPane());
        。mainLayout.row()格()加(freightPanel)。新增(salePanel);
        mainLayout.row()左()增加(新的JSeparator())填写()withOwnRowWidth()。。
        mainLayout.row()中心()加(i5b1)。新增(i5b2)。新增(i5b3)。

        internalFrame.pack();
        internalFrame.setVisible(真正的);

        JFrame的框架=新的JFrame(演示);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane()加(internalFrame)。
        frame.pack();
        frame.setLocationRelativeTo(空);
        frame.setVisible(真正的);
    }

    公共静态无效的主要(字串[] args){
        SwingUtilities.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                新的演示()createAndShowGUI()。
            }
        });
    }
}
 

截图

  import net.java.dev.designgridlayout.Tag;
  import net.java.dev.designgridlayout.DesignGridLayout;
  import javax.swing.table.DefaultTableModel;
  import javax.swing.*;
  import javax.swing.event.*;
  import java.awt.*;
  import java.awt.event.*;

  class alignmentprob
  {
  JFrame JF;
  Container C,C5;
  JDesktopPane JDP;
  JInternalFrame JIF5;
  JLabel i5l1,i5l2,i5l3,i5l4,i5l5,i5l6,i5l7;
  JTextField i5t1,i5t2,i5t3;
  JComboBox<String> i5cb1;
  JButton i5b1,i5b2,i5b3;
  JSeparator i5sep1,i5sep2,i5sep3,i5sep4,i5sep5,i5sep6;
  JTable i5Table1;
  DefaultTableModel i5Model;
  String[] i5Credit = {"A","B"};
  String[] i5ColumnNames = {"Name","Qty","Rate/kg","rate/dzn","total amt."};
  JScrollPane i5t1sp1;

  public alignmentprob()
  {
  JF = new JFrame();
  JDP = new JDesktopPane();
  JF.setVisible(true);
  JF.setSize(800,600);
  JIF5 = new JInternalFrame("Daily Analysis",true,true, true, true);
  C=JF.getContentPane();
  C.add(JDP,BorderLayout.CENTER);
  JIF5.setVisible(true);
  JIF5.setBounds(10, 10, 600, 500);
  C5 = JIF5.getContentPane();
  DesignGridLayout layout5 = new DesignGridLayout(C5);

i5l1 = new JLabel("FREIGHT DETAILS");
i5l2 = new JLabel("Date : ");

i5l3 = new JLabel("SALE DETAILS");
i5l4 = new JLabel("Cash Sales :                             Rs. ");
i5l5 = new JLabel("Credit :  ");

i5l6 = new JLabel("EXPENSES");
i5l7 = new JLabel("Food & Tea :                            Rs. ");


i5t1 = new JTextField(20);
i5t2 = new JTextField(20);
i5t3 = new JTextField(20);

i5cb1 = new JComboBox<String>(i5Credit);

i5b1 = new JButton("Save");
i5b2 = new JButton("Reset");
i5b3 = new JButton("Close");

i5sep1 = new JSeparator();
i5sep2 = new JSeparator();
i5sep3 = new JSeparator();
i5sep4 = new JSeparator();
i5sep5 = new JSeparator();
i5sep6 = new JSeparator();

i5Model = new DefaultTableModel(i5ColumnNames,5);
i5Table1 =new JTable(i5Model){@Override
public boolean isCellEditable(int arg0, int arg1)
{
return false;
}
       };


i5t1sp1 = new JScrollPane(i5Table1);


layout5.row().left().add(i5sep1).fill().withOwnRowWidth();
layout5.row().center().add(i5l1);
layout5.row().left().add(i5sep2).fill().withOwnRowWidth();
layout5.row().grid(i5l2).add(i5t1);

layout5.row().left().add(i5sep3).fill().withOwnRowWidth();
layout5.row().center().add(i5l3);
layout5.row().left().add(i5sep4).fill().withOwnRowWidth();
layout5.row().grid(i5l4).add(i5t2);
layout5.row().grid(i5l5).add(i5cb1);
layout5.row().left().add(i5t1sp1);

layout5.row().left().add(i5sep5).fill().withOwnRowWidth();
layout5.row().center().add(i5l6);
layout5.row().left().add(i5sep6).fill().withOwnRowWidth();
layout5.row().grid(i5l7).add(i5t3);
layout5.row().center().add(i5b1).add(i5b2).add(i5b3);

JDP.add(JIF5);
}

public static void main(String args[])
{
 new alignmentprob();
}
}

My problem is that the textfields are too long and go out of bounds. The table I made consists of 5 rows, but it tends to take up more space. I actually wanted to align the FREIGHT DETAILS & SALES DETAILS in parallel, separated using a vertical line. Please help me with code to do all this, so that my form looks neat and is fully visible.

解决方案

I wasn't able to reproduce the issue based on the code you've posted. Maybe there's something else I'm not seeing that causes this behaviour.

Even when working with third-party layout managers such as DesignGridLayout you can follow a Nested Layout approach using JPanel's to group Freight details and Sales detail components.

About the vertical separator, don't think it's possible with DesignGridLayout. While you can have components spanning several rows it doesn't work with JSeparator.

StackOverflow is not a code factory so in most cases people won't do your job for you but they can provide useful examples. However as I've had to copy all your code to test it and I've made some changes using nested layouts approach, I'll make an exception here. Hope it be helpful:

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import net.java.dev.designgridlayout.DesignGridLayout;

public class Demo {

    private void createAndShowGUI() {

        JLabel i5l1 = new JLabel("FREIGHT DETAILS");
        JLabel i5l2 = new JLabel("Date : ");
        JLabel i5l3 = new JLabel("Vehicle No. : ");
        JLabel i5l4 = new JLabel("From : ");
        JLabel i5l5 = new JLabel("Item : ");
        JLabel i5l6 = new JLabel("Quantity : ");
        JLabel i5l7 = new JLabel("Kg.");
        JLabel i5l8 = new JLabel("Rate :                                          Rs.");
        JLabel i5l15 = new JLabel("SALE DETAILS");
        JLabel i5l16 = new JLabel("Cash Sales :                             Rs. ");
        JLabel i5l17 = new JLabel("Credit :                                       Rs. ");
        JLabel i5l18 = new JLabel("EXPENSES");
        JLabel i5l19 = new JLabel("Food & Tea :                            Rs. ");
        JLabel i5l20 = new JLabel("Wages :                                   Rs. ");
        JLabel i5l21 = new JLabel("Miscellaneous Expenses :     Rs. ");

        JTextField i5t1 = new JTextField(20);
        JTextField i5t2 = new JTextField(20);
        JTextField i5t3 = new JTextField(20);
        JTextField i5t4 = new JTextField(20);
        JTextField i5t11 = new JTextField(20);
        JTextField i5t12 = new JTextField(20);
        JTextField i5t13 = new JTextField(20);
        JTextField i5t14 = new JTextField(20);

        JComboBox i5cb1 = new JComboBox<>();
        JComboBox i5cb2 = new JComboBox<>();
        JComboBox i5cb3 = new JComboBox<>();

        JButton i5b1 = new JButton("Save");
        JButton i5b2 = new JButton("Reset");
        JButton i5b3 = new JButton("Close");

        JSeparator i5sep1 = new JSeparator();
        JSeparator i5sep2 = new JSeparator();
        JSeparator i5sep3 = new JSeparator();
        JSeparator i5sep4 = new JSeparator();
        JSeparator i5sep5 = new JSeparator();
        JSeparator i5sep6 = new JSeparator();

        Object[] columnNames = new Object[]{"Column # 1", "Column # 2", "Column # 3", "Column # 4"};
        DefaultTableModel model = new DefaultTableModel(columnNames, 10);
        JTable table = new JTable(model);

        JScrollPane i5t1sp1 = new JScrollPane(table);

        JPanel freightPanel = new JPanel();
        DesignGridLayout layout1 = new DesignGridLayout(freightPanel);

        layout1.row().left().add(i5sep1).fill().withOwnRowWidth();
        layout1.row().center().add(i5l1);
        layout1.row().left().add(i5sep2).fill().withOwnRowWidth();
        layout1.row().grid(i5l2).add(i5t1);
        layout1.row().grid(i5l3).add(i5t2);
        layout1.row().grid(i5l4).add(i5cb1);
        layout1.row().grid(i5l5).add(i5cb2);
        layout1.row().grid(i5l6).add(i5t3).add(i5l7);
        layout1.row().grid(i5l8).add(i5t4);

        layout1.row().left().add(i5sep5).fill().withOwnRowWidth();
        layout1.row().center().add(i5l18);
        layout1.row().left().add(i5sep6).fill().withOwnRowWidth();
        layout1.row().grid(i5l19).add(i5t12);
        layout1.row().grid(i5l20).add(i5t13);
        layout1.row().grid(i5l21).add(i5t14);

        JPanel salePanel = new JPanel();
        DesignGridLayout layout2 = new DesignGridLayout(salePanel);

        layout2.row().left().add(i5sep3).fill().withOwnRowWidth();
        layout2.row().center().add(i5l15);
        layout2.row().left().add(i5sep4).fill().withOwnRowWidth();
        layout2.row().grid(i5l16).add(i5t11);
        layout2.row().grid(i5l17).add(i5cb3);
        layout2.row().grid().add(i5t1sp1);

        JInternalFrame internalFrame = new JInternalFrame("Daily Analysis",true,true, true, true);

        DesignGridLayout mainLayout = new DesignGridLayout(internalFrame.getContentPane());
        mainLayout.row().grid().add(freightPanel).add(salePanel);
        mainLayout.row().left().add(new JSeparator()).fill().withOwnRowWidth();
        mainLayout.row().center().add(i5b1).add(i5b2).add(i5b3);

        internalFrame.pack();
        internalFrame.setVisible(true);

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(internalFrame);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().createAndShowGUI();
            }
        });
    }
}

Screenshot

这篇关于为什么我的组件出去的界限,请大家帮我必要code对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:25