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