我正在尝试创建一个应用程序,其中列出的数字列在WEST
BorderLayout
区域CENTER
和WEST
中的相应面板上。问题是
JList
区域更宽。现在,它在JPanel
中包含import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
public class Sample1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample1 frame = new Sample1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
contentPane.add(panel, BorderLayout.WEST);
Vector<String> str = new Vector<>();
str.add("1");
str.add("2");
final JList list = new JList();
list.setListData(str);
panel.add(list);
final JPanel panel_1 = new JPanel();
panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new CardLayout(0, 0));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2, "1");
JLabel lblNewLabel = new JLabel("First");
panel_2.add(lblNewLabel);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3, "2");
JLabel lblNewLabel_1 = new JLabel("Second");
panel_3.add(lblNewLabel_1);
list.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){
CardLayout layout = (CardLayout) panel_1.getLayout();
layout.show(panel_1, list.getSelectedValue().toString());
}
});
}
}
,并将其大小调整为默认大小。首选尺寸?不确定!CODE:
{{1}}
答案 0 :(得分:1)
也许使用带有两列的JTable。第二列可以包含关闭按钮。有关此方法的示例,请参阅Table Button Column。
否则你需要在WEST中添加一个面板。该面板将包含JList和JList旁边的一些其他组件,以充当关闭按钮。这种方法的问题是让组件排成一行,按钮知道要关闭哪个面板。
答案 1 :(得分:0)
您是否尝试使用GridLayout
或BoxLayout
尝试使用单独的方法设置GUI。
设置GUI代码:
private void setupGUI() {
JPanel westP = setupWestPane();
getContentPane().add(westPa, BorderLayout.WEST);
}
设置West Pane代码:
private JPanel setupWestPane() {
JPanel westP = setupWestPanel();
JPanel p = new JPanel(new GridLayout(1, 2, 20, 0));
p.setOpaque(false);
p.add(westP);
return p;
}
设置West Panel代码:
private JPanel setupPlayerPanel() {
list1 = new JList();
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// If you want to add a listener to the listbox
// list1.addListSelectionListener(this);
JScrollPane jsp = new JScrollPane(playerList);
JPanel p = new JPanel(new BorderLayout());
p.setOpaque(false);
p.add(new JLabel("List"), BorderLayout.NORTH);
p.add(jsp, BorderLayout.CENTER);
return p;
}
上面的代码只是一个例子。