我正在尝试在Java Swing中实现一个GUI(我是Swing的新手)。我有一个父面板,可以容纳2个面板。左侧面板和右侧面板。左侧面板有一个JComboBox下拉菜单。右侧面板是一张7张牌的CardLayout。我想根据左侧面板中JComboBox的选择更改右侧面板中显示的卡片。所有3个面板都是单独的类:LeftPanel.java,RightPanel.java和ParentPanel.java。我很难找到一个放置动作监听器的地方。 LeftPanel无法看到RightPanel的CardLayout,并且ParentPanel也无法访问子RightPanel的CardLayout。我看到一些关于访问另一个JPanel的CardLayout的帖子,但到目前为止,这些方法都没有对我有用。我的设计有缺陷吗?或者有可能实现这一目标吗?
EDIT 感谢您的建议到目前为止。以下是我的MCVE。请注意,每个类都在自己的(.java)文件中。我这样做,而不是把所有东西都放在Frame类中,只是为了便于维护。我之前在一个班级中拥有所有内容并且工作正常但它成为一个包含2000多行代码和20多个面板的长文件。
//包含Main方法的类MainFrame:
包tempgui; 公共类MainFrame扩展了JFrame {
JFrame Frame1;
public static void main(String[] args){
new MainFrame();
}
public MainFrame(){
Frame1 = new JFrame();
Frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
Frame1.getContentPane().add(new ParentPanel(), BorderLayout.CENTER);
Frame1.setSize(800, 600);
Frame1.setLocationRelativeTo(null);
Frame1.pack();
Frame1.setVisible(true);
}
}
// Class ParentPanel: 公共类ParentPanel扩展了JPanel {
private JPanel ParentPanel;
private LeftPanel LP;
private RightPanel RP;
public ParentPanel(){
ParentPanel = new JPanel();
LP = new LeftPanel();
RP = new RightPanel();
ParentPanel.setLayout(new GridLayout(1,2));
ParentPanel.add(LP);
ParentPanel.add(RP);
add(ParentPanel);
}
}
//类LeftPanel: 公共类LeftPanel扩展了JPanel {
private JPanel LeftPanel;
private JComboBox J1;
public LeftPanel(){
LeftPanel = new JPanel();
String[] Arr = {"RP1","RP2"};
J1 = new JComboBox(Arr);
LeftPanel.add(J1);
add(LeftPanel);
}
}
// Class RightPanel: 公共类RightPanel扩展了JPanel {
private JPanel RightPanel;
private RP1Panel RP1;
private RP2Panel RP2;
private CardLayout C1;
public RightPanel(){
RightPanel = new JPanel();
RP1 = new RP1Panel();
RP2 = new RP2Panel();
C1 = new CardLayout();
RightPanel.setLayout(C1);
RightPanel.add(RP1, "RP1");
RightPanel.add(RP2, "RP2");
C1.show(RightPanel, "RP1");
add(RightPanel);
}
}
//类RP1Panel: 公共类RP1Panel扩展了JPanel {
private JPanel RP1;
private JLabel JRP1;
public RP1Panel(){
RP1 = new JPanel();
JRP1 = new JLabel("RP1 Panel");
RP1.add(JRP1);
add(RP1);
}
}
//类RP2Panel: 公共类RP2Panel扩展了JPanel {
private JPanel RP2;
private JLabel JRP2;
public RP2Panel(){
RP2 = new JPanel();
JRP2 = new JLabel("RP2 Panel");
RP2.add(JRP2);
add(RP2);
}
}
答案 0 :(得分:1)
我会尝试给我的类方法允许其他类从中提取“ state ”信息,例如组合框的状态,并允许其他类改变它们,例如允许将侦听器添加到组合框中。例如:
// Class RightPanel:
class RightPanel extends JPanel {
private JPanel rightPanel;
private RP1Panel rightPanel1;
private RP2Panel rightPanel2;
private CardLayout cardLayout;
public RightPanel() {
rightPanel = new JPanel();
rightPanel1 = new RP1Panel();
rightPanel2 = new RP2Panel();
cardLayout = new CardLayout();
rightPanel.setLayout(cardLayout);
rightPanel.add(rightPanel1, RP1Panel.NAME); //!!
rightPanel.add(rightPanel2, RP2Panel.NAME); // !!
cardLayout.show(rightPanel, RP1Panel.NAME); // !!
add(rightPanel);
}
// !!
public void showCard(String name) {
cardLayout.show(rightPanel, name);
}
}
然后其他类可以侦听组合框的更改,并在需要时提取选择。右侧面板同样具有允许更改其显示的“卡片”JPanel的方法。类似的东西:
// Class ParentPanel:
class ParentPanel extends JPanel {
private JPanel ParentPanel;
private LeftPanel leftPanel;
private RightPanel rightPanel;
public ParentPanel() {
ParentPanel = new JPanel();
leftPanel = new LeftPanel(new String[] {RP1Panel.NAME, RP2Panel.NAME});
rightPanel = new RightPanel();
ParentPanel.setLayout(new GridLayout(1, 2));
ParentPanel.add(leftPanel);
ParentPanel.add(rightPanel);
add(ParentPanel);
// !!
leftPanel.comboAddActionListener(e -> {
String selection = leftPanel.getComboSelection();
rightPanel.showCard(selection);
});
}
}
他们可以在主要人物中捆绑在一起:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FooGui {
public static void main(String[] args) {
new MainFrame();
}
}
class MainFrame extends JFrame {
JFrame Frame1;
public MainFrame() {
Frame1 = new JFrame();
Frame1.setDefaultCloseOperation(EXIT_ON_CLOSE);
Frame1.getContentPane().add(new ParentPanel(), BorderLayout.CENTER);
Frame1.setSize(800, 600);
Frame1.setLocationRelativeTo(null);
Frame1.pack();
Frame1.setVisible(true);
}
}
// Class ParentPanel:
class ParentPanel extends JPanel {
private JPanel ParentPanel;
private LeftPanel leftPanel;
private RightPanel rightPanel;
public ParentPanel() {
ParentPanel = new JPanel();
leftPanel = new LeftPanel(new String[] {RP1Panel.NAME, RP2Panel.NAME});
rightPanel = new RightPanel();
ParentPanel.setLayout(new GridLayout(1, 2));
ParentPanel.add(leftPanel);
ParentPanel.add(rightPanel);
add(ParentPanel);
// !!
leftPanel.comboAddActionListener(e -> {
String selection = leftPanel.getComboSelection();
rightPanel.showCard(selection);
});
}
}
// Class LeftPanel:
class LeftPanel extends JPanel {
private JPanel leftPanel;
private JComboBox<String> comboBox; //!!
// !!
public LeftPanel(String[] comboTexts) {
leftPanel = new JPanel();
comboBox = new JComboBox<>(comboTexts); // !!
leftPanel.add(comboBox);
add(leftPanel);
}
// !!
public String getComboSelection() {
return comboBox.getSelectedItem().toString();
}
// !!
public void comboAddActionListener(ActionListener listener) {
comboBox.addActionListener(listener);
}
}
// Class RightPanel:
class RightPanel extends JPanel {
private JPanel rightPanel;
private RP1Panel rightPanel1;
private RP2Panel rightPanel2;
private CardLayout cardLayout;
public RightPanel() {
rightPanel = new JPanel();
rightPanel1 = new RP1Panel();
rightPanel2 = new RP2Panel();
cardLayout = new CardLayout();
rightPanel.setLayout(cardLayout);
rightPanel.add(rightPanel1, RP1Panel.NAME); //!!
rightPanel.add(rightPanel2, RP2Panel.NAME); // !!
cardLayout.show(rightPanel, RP1Panel.NAME); // !!
add(rightPanel);
}
// !!
public void showCard(String name) {
cardLayout.show(rightPanel, name);
}
}
// Class RP1Panel:
class RP1Panel extends JPanel {
// !!
public static final String NAME = "right panel 1";
private JPanel RP1;
private JLabel JRP1;
public RP1Panel() {
setName(NAME);
RP1 = new JPanel();
JRP1 = new JLabel("RP1 Panel");
RP1.add(JRP1);
add(RP1);
}
}
// Class RP2Panel:
class RP2Panel extends JPanel {
// !!
public static final String NAME = "right panel 2";
private JPanel RP2;
private JLabel JRP2;
public RP2Panel() {
setName(NAME);
RP2 = new JPanel();
JRP2 = new JLabel("RP2 Panel");
RP2.add(JRP2);
add(RP2);
}
}
整个MCVE看起来像:
var alldb = (from r in MasterDataFactory.Container.ComponentRegistry.Registrations
let s = r.Services.Where(i => i is KeyedService).Select(i => i as KeyedService).FirstOrDefault()
where s!=null && s.ServiceType==typeof(ISomeInterface)
select s).ToList();