现在我知道有很多这方面的问题,我已经阅读了十几个。但我只是碰壁,我无法做出正面或反面。
继承我的问题。
我有3个Panel类。
ConfigurePanel.java
ConnectServerPanel.java
RunServerPanel.java
和我的JFrame类
StartUPGUI.java
这是在启动时初始化的内容
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
startUp = new sjdproject.GUI.ConfigurePanel();
runServer = new sjdproject.GUI.RunServerPanel();
serverConnect = new sjdproject.GUI.ConnectServerPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
jPanel1.setLayout(new java.awt.CardLayout());
jPanel1.add(startUp, "card2");
jPanel1.add(runServer, "card4");
jPanel1.add(serverConnect, "card3");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(27, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(30, Short.MAX_VALUE))
);
我的StartUPGUI首先调用StartUpPanel。在我的StartUpPanel.java中,我有一个调用StartUPGUI中的setPanel方法的按钮
StartUpGUI s = new StartUpGUI();
String str = "";
if(runserverbtn.isSelected()){
str = "runserver";
}
else if(connectserverbtn.isSelected()){
str = "connectserver";
}
else{
str = "";
}
s.setPanel(str);
这是我的setPanel方法
void setPanel(String str){
if(str == "runserver"){
}
else if(str == "connectserver"){
}
else{
}
}
我需要在if块中放置什么才能更改面板视图?我会假设jPanel1.something(),但我不知道那是什么。
答案 0 :(得分:4)
“我需要在if块中放置什么来更改面板视图?我会假设jPanel1.something()但我不知道那是什么。”
请勿将字符串与==
进行比较,否则无效。使用.equals
.. if("runserver".equals(str)){
您需要使用.show
CardLayout
CardLayout.show(jPanel1, "whichPanel");
public void show(Container parent, String name)
- 使用addLayoutComponent翻转到使用指定名称添加到此布局的组件。如果不存在这样的组件,则没有任何反应。
void setPanel(String str){
CardLayout layout = (CardLayout)jPanel1.getLayout();
if("runserver".equals(str)){
layout.show(jPanel1, "card4");
}else if("connectserver".equals(str)){
layout.show(jPanel1, "card3");
} else{
layout.show(jPanel1, "card2");
}
}
有关详细信息,请参阅How to Use CardLayout,有关更多方法,请参阅API。
更新
尝试运行此示例并使用您的代码检查它,看看您是否注意到任何有用的内容
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestCardLayout {
PanelOne p1 = new PanelOne();
PanelTwo p2 = new PanelTwo();
PanelThree p3 = new PanelThree();
CardLayout layout = new CardLayout();
JPanel cardPanel = new JPanel(layout);
public TestCardLayout() {
JButton showOne = new JButton("Show One");
JButton showTwo = new JButton("Show Two");
JButton showThree = new JButton("Show Trree");
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(showOne);
buttonsPanel.add(showTwo);
buttonsPanel.add(showThree);
showOne.addActionListener(new ButtonListener());
showTwo.addActionListener(new ButtonListener());
showThree.addActionListener(new ButtonListener());
cardPanel.add(p1, "panel 1");
cardPanel.add(p2, "panel 2");
cardPanel.add(p3, "panel 3");
JFrame frame = new JFrame("Test Card");
frame.add(cardPanel);
frame.add(buttonsPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("Show One".equals(command)) {
layout.show(cardPanel, "panel 1");
} else if ("Show Two".equals(command)) {
layout.show(cardPanel, "panel 2");
} else {
layout.show(cardPanel, "panel 3");
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestCardLayout testCardLayout = new TestCardLayout();
}
});
}
}
class PanelOne extends JPanel {
public PanelOne() {
setBackground(Color.GREEN);
add(new JLabel("Panel one"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
class PanelTwo extends JPanel {
public PanelTwo() {
setBackground(Color.BLUE);
add(new JLabel("Panel two"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
class PanelThree extends JPanel {
public PanelThree() {
setBackground(Color.YELLOW);
add(new JLabel("Panel three"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
更新2
问题是,该按钮位于ConfigurePanel
类中。尝试在该类中创建新的StartUPGUI
,不会引用相同的组件。您需要做的是将StartUPGUI
的引用传递给ConfigurePanel
。像这样的东西
public class ConfigurePanel extends JPanel {
StartUPGUI startup;
public ConfigurePanel(StartUPGUI startup) {
this.startup = startup;
}
....
public void actionPerformed(ActionEvent e) {
startup.setPanel("runserver");
}
}
从ConfigurePanel
像这样
StartUPGUI
new ConfigurePanel(StartUPGUI.this);