0

有一个简单的应用程序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);
    }
}
4

1 回答 1

4

您需要使用布局管理器的组合(如@AndrewThompson 在上面的评论中所述)来实现与null layout.

避免使用null layout,请参阅:Null 布局是邪恶的,为什么不赞成在 Swing 中使用 null 布局?

使用下面的代码,您可以获得与setBounds()方法相同的输出。我没有ActionListener在这段代码中添加一个,因为我只是在演示如何停止使用null layout并获得相同的输出。是的!第二个似乎更短,这是因为如果您想要/需要特定的窗口大小,pack()您仍然可以。setSize()

要在下面添加更多元素,只需添加更多JPanels 并将它们添加到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);
    }
}
于 2016-07-03T21:06:49.633 回答