簡體   English   中英

使用了JScrollPane,所以我無法設置其他JComponent的邊界

[英]JScrollPane used so I can't set bounds of other JComponents

有一個簡單的應用程序,它使用JScrollPane處理2個列表,並具有1個按鈕來切換它們。 我想添加更多Swing元素,但是無法使用object.setBounds移動它們。 無論我在此方法元素中寫什么,都不會改變其位置和大小。

package paka;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;;
public class Question extends JFrame {
    private JList leftlist,rightlist;
    private JButton movebutton;
    private JLabel pointlessLabel;
    private static String[] foods={"bacon","wings","ham","beef","more bacon"};

    public Question(){
        super("title");
        setLayout(new FlowLayout());

        leftlist=new JList(foods);
        leftlist.setVisibleRowCount(3);
        leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        add(new JScrollPane(leftlist));
        movebutton = new JButton("Move");

        movebutton.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        rightlist.setListData(leftlist.getSelectedValues());

                    }
                }
                );
        add(movebutton);

        rightlist = new JList();
        rightlist.setVisibleRowCount(3);
        rightlist.setFixedCellWidth(100);rightlist.setFixedCellHeight(15);
        rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        add(new JScrollPane(rightlist));        

        //Okay, deleting everything below we have only two list with button that moves elements from 1st list to 2nd

        movebutton = new JButton("Click me!");
        movebutton.setBounds(700, 100, 80, 20);
        add(movebutton);

        pointlessLabel = new JLabel("I'm unbreakable");
        pointlessLabel.setBounds(500,200,100,50);
        add(pointlessLabel);



    }

    public static void main(String args[]){
        Question go = new Question();
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.setSize(300,200);
        go.setVisible(true);
    }
}

您需要使用布局管理器的組合(如@AndrewThompson在上面的評論中所述),以實現與null layout相同的輸出。

避免使用null layout ,請參閱: null布局是邪惡的為什么在Swing中使用null布局卻不受歡迎?

使用下面的代碼,您可以擁有與setBounds()方法相同的輸出。 我沒有在此代碼中添加ActionListener ,因為我只是在演示如何停止使用null layout並具有相同的輸出。 是! 第二個似乎較短,這是由於pack()但是如果您希望/需要特定的窗口大小,仍可以使用setSize()

要在下面添加更多元素,只需添加更多JPanel並將它們添加到pane ,我希望這有助於解決您的問題,如果沒有,請發布一個Minimal Complete and Verifiable Example實例 ,我們可以復制粘貼,也可以簡短說明,但仍然顯示您的問題並遵循上述建議

重要的提示:

我將引用來自@MadProgrammer的此答案的注釋 ,因為我使用了prototypeCellValue來使rightList的寬度更短:

我還要對prototypeCellValue保持謹慎,除非該值與您的數據的預期長度匹配,否則在顯示時它可能會截斷您的數據,只需要小心

在此處輸入圖片說明

import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

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

    public static void createAndShowGUI() {
        String[] foods = { "bacon", "wings", "ham", "beef", "more bacon" };

        JFrame frame;
        JPanel topPane, bottomPane, pane;
        JList leftList, rightList;
        JButton moveButton, clicMeButton;
        JScrollPane scroll;
        JLabel label;

        frame = new JFrame("title");
        topPane = new JPanel();
        bottomPane = new JPanel();
        pane = new JPanel();
        leftList = new JList(foods);
        rightList = new JList();
        moveButton = new JButton("Move");
        clicMeButton = new JButton("Click me!");
        label = new JLabel("I'm unbreakable");

        leftList.setVisibleRowCount(3);
        rightList.setVisibleRowCount(3);
        leftList.setPrototypeCellValue(String.format("%30s", ""));
        rightList.setPrototypeCellValue(String.format("%30s", ""));

        pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
        topPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        scroll = new JScrollPane(leftList);

        topPane.add(scroll);
        topPane.add(moveButton);

        scroll = new JScrollPane(rightList);
        topPane.add(scroll);

        bottomPane.setLayout(new FlowLayout());
        bottomPane.add(clicMeButton);
        bottomPane.add(label);

        pane.add(topPane);
        pane.add(bottomPane);
        frame.add(pane);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

暫無
暫無

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

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