簡體   English   中英

Java布局挫敗

[英]Java Layout Frustration

我需要有關GUI布局的指導

要將其縮小到要點:

  • 我有三個主要的JPanels(信息部分,操作和數據結構)
  • 如果不更改JLabel,則無法填充這些內容
  • 我需要一個子面板來進行網格布局的操作(無法正常工作,現在真的很煩我)
  • 我需要它看起來像下面的圖片
  • 紅色分隔線是可選的,使其更整潔

我的下一步是實現堆棧,但我首先要使其看起來正常。

https://i.stack.imgur.com/R7G2g.jpg

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class StackPanel extends JPanel {

    JPanel west, westSub1, east, south, southSub1;
    JTextArea infoText, popText, pushText, peekText, resultText;
    JLabel aTitle, bTitle, cTitle, Result;
    JButton push, pop, peek, test;

    public StackPanel() {        

        // Creating JPanels
        setLayout(new BorderLayout());
        west = new JPanel();
        westSub1 = new JPanel(new GridLayout(3,2));
        east = new JPanel();
        south = new JPanel();
        west.add(westSub1);

        // Creating JLabels / JTextArea
        aTitle = new JLabel("Operations");
        bTitle = new JLabel("Data Structure Contents");
        cTitle = new JLabel("Information");
        infoText = new JTextArea("This is where commands will be displayed.");
        pushText = new JTextArea("pushtxt");
        popText = new JTextArea("poptxt");
        peekText = new JTextArea("g");
        resultText = new JTextArea("");
        west.add(aTitle);
        westSub1.add(pushText);
        westSub1.add(popText);
        westSub1.add(peekText);
        westSub1.add(resultText);
        east.add(bTitle);
        south.add(cTitle);
        south.add(infoText);

        // Creating & Adding JButtons
        push = new JButton("PUSH");
        pop = new JButton("POP") ;
        peek = new JButton("PEEK");
        test = new JButton("TEST");
        westSub1.add(push);
        westSub1.add(pop);
        westSub1.add(peek);
        westSub1.add(test);

        // Setting the placements of GUI objects
        add(west, BorderLayout.WEST);
        add(east, BorderLayout.CENTER);
        add(south, BorderLayout.SOUTH);

        // Declaring JPanel sizes // Width|Height
        west.setPreferredSize(new Dimension(200,200));
        east.setPreferredSize(new Dimension(400,100));
        south.setPreferredSize(new Dimension(100,150));

        // Setting black borders for JPanels
        west.setBorder(BorderFactory.createLineBorder(Color.black));
        east.setBorder(BorderFactory.createLineBorder(Color.black));
        south.setBorder(BorderFactory.createLineBorder(Color.black));

        // Setting JPanel background colours
        west.setBackground(new Color(234,237,242));
        east.setBackground(new Color(255,255,255));
        south.setBackground(new Color(240,240,240));
    }

}

也許可以使用TitledBorder來代替在西/東/南面板的頂部使用標簽。 這將在面板周圍放置一條矩形線,頂部帶有標題。

閱讀Swing教程中有關如何使用邊框的部分, 獲取更多信息和可行示例。

如果您不想這樣做,則可能需要將默認的FlowLayout或每個面板更改為另一個布局。 例如,您可以使用BorderLayout 然后將標簽添加到PAGE_START ,將其他組件添加到CENTER 要點是您可以嵌套具有不同布局的面板以實現所需的布局。

這是一些讓您入門的東西。 請閱讀評論,如有需要,請隨時澄清。 我並沒有做所有需要的布局更改:我只是想演示應該做些什么,所以您明白了。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class StackPanel extends JPanel {

    JPanel west, westSub1, east, south, southSub1;
    JTextArea infoText, popText, pushText, peekText, resultText;
    JLabel aTitle, bTitle, cTitle, Result;
    JButton push, pop, peek, test;

    public StackPanel() {

        // Creating JPanels
        setLayout(new BorderLayout());

        // Creating JLabels / JTextArea
        aTitle = new JLabel("Operations");
        bTitle = new JLabel("Data Structure Contents");

        west = new JPanel();
        //you need to set layout manager to the panel, to lay out its components
        west.setLayout(new BorderLayout());
        west.setPreferredSize(new Dimension(200,200));
        west.setBorder(BorderFactory.createLineBorder(Color.black));
        west.setBackground(new Color(234,237,242));
        add(west, BorderLayout.WEST);

        west.add(aTitle, BorderLayout.NORTH);//use panel's layout manager

        //you have 4 rows so GridLayout(3,2) is wrong
        westSub1 = new JPanel(new GridLayout(4,2));

        //for a grid layout: add components in the right order
        push = new JButton("PUSH");
        westSub1.add(push);

        pushText = new JTextArea("pushtxt");
        westSub1.add(pushText);

        pop = new JButton("POP") ;
        westSub1.add(pop);

        popText = new JTextArea("poptxt");
        westSub1.add(popText);

        peek = new JButton("PEEK");
        westSub1.add(peek);

        peekText = new JTextArea("g");
        westSub1.add(peekText);

        test = new JButton("TEST");
        westSub1.add(test);

        resultText = new JTextArea("");
        westSub1.add(resultText);

        west.add(westSub1, BorderLayout.CENTER);//use panel's layout manager

        east = new JPanel();
        east.setPreferredSize(new Dimension(400,100));
        east.setBorder(BorderFactory.createLineBorder(Color.black));
        east.setBackground(new Color(255,255,255));

        east.add(bTitle);
        add(east, BorderLayout.CENTER);

        south = new JPanel();
        //you need to set layout manager to the panel, to lay out its components
        south.setLayout(new BorderLayout());
        south.setPreferredSize(new Dimension(100,150));
        south.setBorder(BorderFactory.createLineBorder(Color.black));
        south.setBackground(new Color(240,240,240));
        add(south, BorderLayout.SOUTH);

        cTitle = new JLabel("Information");
        south.add(cTitle, BorderLayout.NORTH); //use panel's layout manager
        infoText = new JTextArea("This is where commands will be displayed.");
        south.add(infoText, BorderLayout.CENTER);
    }

    public static void main(String[] args){

          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

          frame.getContentPane().add(new StackPanel());
          frame.pack();
          frame.setVisible(true);
    }
}

該代碼具有所需的所有導入,就像運行它的main一樣。
有關應如何發布代碼的更多信息,請閱讀: https : //stackoverflow.com/help/mcve

很久以前,我與Swing合作。 但是我對Swing的默認布局也有同樣的麻煩。 我的提示是更改為TableLayout。 它真的很容易使用,您可以獲得確切的結果。

請檢查該教程: http : //www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html

並不完美,但我會碰到這樣的代碼

public class StackPanel extends JPanel {

JPanel west, east, south, southSub1;
JTextArea infoText, popText, pushText, peekText, resultText;
JLabel aTitle, bTitle, cTitle, Result;
JButton push, pop, peek, test;

public StackPanel() {

    // Creating JPanels
    double size[][] =  {{0.3, 0.7}, {TableLayout.FILL, 70}};
    setLayout(new TableLayout(size));

    double sizeWest[][] =  {{0.5, 0.5}, {20, 20, 20, 20, 20, 20}};
    setLayout(new TableLayout(size));

    west = new JPanel(new TableLayout(sizeWest));


    east = new JPanel();
    south = new JPanel();


    // Creating JLabels / JTextArea
    aTitle = new JLabel("Operations");
    bTitle = new JLabel("Data Structure Contents");
    cTitle = new JLabel("Information");
    infoText = new JTextArea("This is where commands will be displayed.");
    pushText = new JTextArea("pushtxt");
    popText = new JTextArea("poptxt");
    peekText = new JTextArea("g");
    resultText = new JTextArea("");
    west.add(aTitle, "0,0,1,0");
    west.add(pushText, "0,1");
    west.add(popText, "0,2");
    west.add(peekText, "0,3");
    west.add(resultText, "0,4");
    east.add(bTitle);
    south.add(cTitle);
    south.add(infoText);

    // Creating & Adding JButtons
    push = new JButton("PUSH");
    pop = new JButton("POP") ;
    peek = new JButton("PEEK");
    test = new JButton("TEST");
    west.add(push, "1,1");
    west.add(pop,"1,2");
    west.add(peek,"1,3");
    west.add(test, "1,4");

    // Setting the placements of GUI objects
    add(west, "0,0");
    add(east, "1,0");
    add(south, "0,1, 1,1");

    // Declaring JPanel sizes // Width|Height
    west.setPreferredSize(new Dimension(200,200));
    east.setPreferredSize(new Dimension(400,100));
    south.setPreferredSize(new Dimension(100,150));

    // Setting black borders for JPanels
    west.setBorder(BorderFactory.createLineBorder(Color.black));
    east.setBorder(BorderFactory.createLineBorder(Color.black));
    south.setBorder(BorderFactory.createLineBorder(Color.black));

    // Setting JPanel background colours
    west.setBackground(new Color(234,237,242));
    east.setBackground(new Color(255,255,255));
    south.setBackground(new Color(240,240,240));
}}

希望這對您有用!

基本的Swing布局管理器要么非常初級,要么使用起來非常復雜。 因此, FormLayoutMigLayout可能是一個使用選項,它們很復雜,但使用起來並不太復雜。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM