Java Applet JPanel中项目的对齐方式

时间:2015-02-13 00:20:37

标签: java swing jpanel layout-manager grouplayout

enter image description here

我试图让右上方的宽度与下面的两个宽度相同,我也想让下面的JTextArea与宽度相匹配。有什么想法吗?

似乎无论我设定的尺寸是什么,它都在做它想要的任何事情。例如,Output JTextArea仅设置为一列。顶部图像为(700x250),两个半图像为(350x250)。

public class MyApplet extends Applet{
    private static final long serialVersionUID = 1L;
    private JTextArea input_data;
    private JTextArea input_jmax;
    private JTextArea input_gibbs;
    private JTextArea input_burnin;
    private JTextArea output_text;
    private JLabel output_graph;
    private JLabel output_burn1;
    private JLabel output_burn2;
    private static Graphics g=null;

    public void init () {
        //INPUT
        this.input_data = new JTextArea("Enter Data", 30, 30);
        JScrollPane data_pane= new JScrollPane(input_data);
        this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
        this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
        this.input_burnin = new JTextArea("Burnin", 1, 30);
        //OUTPUT
        Dimension D;
        D = new Dimension(700, 250);
        Image start;

        this.output_text = new JTextArea("####################Output####################",15,1);
        this.output_text.setEditable(false);
        JScrollPane output_pane= new JScrollPane(output_text);

        this.output_burn1 = new JLabel();
        D = new Dimension(345,250);
        start = createImage((int) D.getWidth(), (int) D.getHeight());
        g = start.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,345,250);
        output_burn1.setIcon(new ImageIcon(start));

        this.output_burn2 = new JLabel();
        D = new Dimension(345, 250);
        start = createImage((int) D.getWidth(), (int) D.getHeight());
        g = start.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,345,250);
        output_burn2.setIcon(new ImageIcon(start));

        this.output_graph = new JLabel();
        start = createImage((int) D.getWidth(), (int) D.getHeight());
        g = start.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,700,250);
        output_graph.setIcon(new ImageIcon(start));

        //BUTTON
        JButton b = new JButton("Process Data");

        //set size

        setSize(1200, 600);
        setBackground(Color.lightGray);

        JPanel burninPanel = new JPanel();
        burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
        burninPanel.add(output_burn1);
        burninPanel.add(Box.createHorizontalStrut(10));
        burninPanel.add(output_burn2);
        /*
        //Create Input Side
         * */
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
        inputPanel.setBackground(Color.LIGHT_GRAY);
        inputPanel.add(data_pane);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(input_jmax);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(input_gibbs);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(input_burnin);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(b);

        //Create Output Side
        JPanel outputPanel = new JPanel();
        outputPanel.setBackground(Color.lightGray);
        outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
        outputPanel.add(output_graph);
        outputPanel.add(Box.createVerticalStrut(10));
        outputPanel.add(burninPanel);
        outputPanel.add(Box.createVerticalStrut(10));
        outputPanel.add(output_pane);


        this.setLayout(new GridLayout(1, 2));
        this.setVisible(true); 
        GroupLayout layout = new GroupLayout(this);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createSequentialGroup()
                        .addComponent(inputPanel)
                        .addComponent(outputPanel)
                        ) 
                );
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup()
                        .addComponent(inputPanel)
                        .addComponent(outputPanel)
                        )
                );

        this.setLayout(layout);        
        this.setVisible(true);

        // specify that action events sent by the
        // button or the input TextField should be handled 
        // by the same CapitalizerAction object
        Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph);
        b.addActionListener(ca);
        //this.input.addActionListener(ca);
    }
}

1 个答案:

答案 0 :(得分:0)

事实证明,您无法将相同大小的图像添加到堆栈中。我没有将所有项目放在“outputPanel”中,而是将每个项目放在自己的面板中,并将所有面板添加到outputPanel。这么蠢!“

public class MyApplet extends Applet{
    private static final long serialVersionUID = 1L;
    private JTextArea input_data;
    private JTextArea input_jmax;
    private JTextArea input_gibbs;
    private JTextArea input_burnin;
    private JTextArea output_text;
    private JLabel output_graph;
    private JLabel output_burn1;
    private JLabel output_burn2;
    private static Graphics g=null;

    public void init () {
        //INPUT
        this.input_data = new JTextArea("Data", 30, 30);
        JScrollPane data_pane= new JScrollPane(input_data);
        this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
        this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
        this.input_burnin = new JTextArea("Burn-in", 1, 30);


        //OUTPUT
        Dimension D;
        Image start;

        this.output_burn1 = new JLabel();
        D = new Dimension(345,200);
        start = createImage((int) D.getWidth(), (int) D.getHeight());
        g = start.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,345,200);
        output_burn1.setIcon(new ImageIcon(start));

        this.output_burn2 = new JLabel();
        D = new Dimension(345, 200);
        start = createImage((int) D.getWidth(), (int) D.getHeight());
        g = start.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,345,200);
        output_burn2.setIcon(new ImageIcon(start));

        this.output_graph = new JLabel();
        D = new Dimension(700, 200);
        start = createImage((int) D.getWidth(), (int) D.getHeight());
        g = start.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,700,200);
        output_graph.setIcon(new ImageIcon(start));

        //BUTTON
        JButton b = new JButton("Process Data");

        //set size
        setSize(1200, 600);
        setBackground(Color.lightGray);

        //Create Input Side
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(Color.LIGHT_GRAY);
        buttonPanel.add(b);
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
        inputPanel.setBackground(Color.LIGHT_GRAY);
        inputPanel.add(data_pane);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(input_jmax);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(input_gibbs);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(input_burnin);
        inputPanel.add(Box.createVerticalStrut(10));
        inputPanel.add(buttonPanel);

        //Create Output Side
        JPanel estPanel = new JPanel();
        estPanel.setBackground(Color.LIGHT_GRAY);
        estPanel.add(output_graph);

        JPanel burninPanel = new JPanel();
        burninPanel.setBackground(Color.LIGHT_GRAY);
        burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
        burninPanel.add(output_burn1);
        burninPanel.add(Box.createHorizontalStrut(10));
        burninPanel.add(output_burn2);

        this.output_text = new JTextArea("",15,1);
        this.output_text.setEditable(false);
        JScrollPane output_pane= new JScrollPane(output_text);

        JPanel outputPanel = new JPanel();
        outputPanel.setBackground(Color.lightGray);
        outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
        outputPanel.add(estPanel);
        outputPanel.add(Box.createVerticalStrut(10));
        outputPanel.add(burninPanel);
        outputPanel.add(Box.createVerticalStrut(10));
        outputPanel.add(output_pane);


        this.setVisible(true); 
        GroupLayout layout = new GroupLayout(this);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createSequentialGroup()
                        .addComponent(inputPanel)
                        .addComponent(outputPanel)
                        ) 
                );
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(LEADING)
                        .addComponent(inputPanel)
                        .addComponent(outputPanel)
                        )
                );
        this.setLayout(layout);        
        this.setVisible(true);

        // specify that action events sent by the
        // button or the input TextField should be handled 
        // by the same CapitalizerAction object
        Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph,output_burn1,output_burn2);
        b.addActionListener(ca);
    }
}