[英]how to show panels of other classes in cardlayout?
我已經創建了這個簡單的程序。 我的問題是我想顯示其他類的面板而不打開新的JFrame。 但我無法創建任何解決方案。 有人可以編寫簡單的代碼來顯示如何訪問其他類的面板並替換當前面板嗎?
package cardlayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Clayou {
private JFrame jf = new JFrame("card layout");
private JPanel jp = new JPanel();
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JButton jb1 = new JButton("second");
private JButton jb2 = new JButton("first");
private CardLayout cl = new CardLayout();
Clayou() {
jp.setLayout(cl);
jp1.add(jb1);
jp2.add(jb2);
jp1.setBackground(Color.black);
jp2.setBackground(Color.white);
jp.add(jp1, "1");
jp.add(jp2, "2");
cl.show(jp, "1");
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(jp, "2");
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(jp, "1");
}
});
jf.add(jp);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Clayou();
}
});
}
}
我只是回答了一個可能與此答案相同的問題。 更改gui中的面板我認為這也會對您有所幫助。 您可以擴展卡類,以允許更多卡類型。 在此示例中,我制作了兩種卡類型。
進口
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.util.Vector;
主班
public class Gui extends JFrame implements ListSelectionListener {
Vector<String> menuList = new Vector<>();
JList<String> menu = new JList<>(menuList);
JPanel content = new JPanel(new CardLayout());
Gui(){
//put menu on left, content in middle
add(menu, BorderLayout.WEST);
add(content, BorderLayout.CENTER);
menu.addListSelectionListener(this);
//add multiple cards
addCard(new SimpleLabelCard("First Item", "First Item Text"));
addCard(new SimpleLabelCard("Second Item", "Second Item Text"));
addCard(new SimpleTextAreaCard("Third Item", "Third Item Text"));
//set content to first item
((CardLayout) content.getLayout()).show(content, "First Item");
}
private void addCard(Card c){
menuList.add(c.name);
content.add(c, c.name);
}
public static void main(String [] args){
Gui gui = new Gui();
gui.setSize(600, 500);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
@Override
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting()) return;
//set card layout from JList menu
((CardLayout) content.getLayout()).show(content, menu.getSelectedValue());
}
}
其他類,將每個類放在各自的文件中。
//Card.java
//this is the basic card class the extends JPanel
//it contains the name so you can easily switch to it
public class Card extends JPanel{
public final String name;
public Card(String name){
this.name = name;
}
}
//SimpleLabelCard.java
//extends Card, so it is also a JPanel
public class SimpleLabelCard extends Card{
private JLabel label = new JLabel();
public SimpleLabelCard(String name, String text) {
super(name);
label.setText(text);
add(label);
}
}
//SimpleTextAreaCard.java
public class SimpleTextAreaCard extends Card{
private JTextArea text = new JTextArea();
public SimpleTextAreaCard(String name, String text) {
super(name);
this.text.setText(text);
setLayout(new BorderLayout());
add(this.text, BorderLayout.CENTER);
}
}
現在,如果您要制作其他類型的卡,只需擴展卡類別。 我希望這很簡單。 對我而言,這實際上是一次學習經歷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.