当前位置: 首页 > 知识库问答 >
问题:

如何用组合框在卡片之间切换[重复]

符国安
2023-03-14

我正在尝试使用Jcombobox“费用”在卡片面板之间进行更改。有人能告诉我我做错了什么吗?我使面板正确地出现,但当我看到费用j选项卡式窗格时,第一张卡就显示出来了。一旦我试图在它们之间切换,控制台就会充满错误。我注意到所有的卡片都在同时显示,但它们是重叠的。1)如何一次只显示一张牌(面板)?2)如何让JComboBox在牌(面板)之间切换?

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.JList;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JButton; 
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;

public class LanaFrame extends JFrame implements ItemListener{

private JPanel contentPane, cards;
private JList driverlist;
private JComboBox expenses;
final static String FUEL= "Fuel";
final static String TOLL= "Toll";
final static String REPAIR="Repair";
private static String[] comboboxitems= {FUEL,TOLL,REPAIR};
private JTextField textFieldFirstName;
private JTextField textFieldLastName;
private JTextField textFieldTruckNumber;
private JTextField textFieldTrailerNumber;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LanaFrame frame = new LanaFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public LanaFrame() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 500);
    setResizable(false);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    tabbedPane.setBounds(5, 5, 775, 480);
    contentPane.add(tabbedPane);

    //Drivers Panel
    JPanel Drivers = new JPanel();
    tabbedPane.addTab("Drivers", null, Drivers, null);
    Drivers.setLayout(null);

    driverlist= new JList(comboboxitems);
    driverlist.setBackground(Color.WHITE);
    driverlist.setVisibleRowCount(4);
    driverlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    Drivers.add(driverlist);
    driverlist.setBounds(482,6,266,211);

    JButton btnAddDriver = new JButton("Add Driver");
    btnAddDriver.setBounds(5, 285, 117, 29);
    Drivers.add(btnAddDriver);

    JTextArea driverArea = new JTextArea();
    driverArea.setBackground(Color.LIGHT_GRAY);
    driverArea.setBounds(482, 217, 266, 211);
    Drivers.add(driverArea);
    driverArea.setEditable(false);


    JTextPane txtpnFirstName = new JTextPane();
    txtpnFirstName.setText("First name");
    txtpnFirstName.setBounds(5, 20, 140, 15);
    Drivers.add(txtpnFirstName);
    txtpnFirstName.setEditable(false);

    textFieldFirstName = new JTextField();
    textFieldFirstName.setBounds(5, 40, 140, 30);
    Drivers.add(textFieldFirstName);
    textFieldFirstName.setColumns(10);

    JTextPane txtpnLastName = new JTextPane();
    txtpnLastName.setText("Last Name");
    txtpnLastName.setBounds(5, 75, 140, 15);
    Drivers.add(txtpnLastName);
    txtpnLastName.setEditable(false);

    textFieldLastName = new JTextField();
    textFieldLastName.setBounds(5, 95, 140, 30);
    Drivers.add(textFieldLastName);
    textFieldLastName.setColumns(10);

    JTextPane txtpnTruckNumber = new JTextPane();
    txtpnTruckNumber.setText("Truck Number");
    txtpnTruckNumber.setBounds(5, 130, 140, 15);
    Drivers.add(txtpnTruckNumber);
    txtpnTruckNumber.setEditable(false);

    textFieldTruckNumber = new JTextField();
    textFieldTruckNumber.setBounds(5, 150, 140, 30);
    Drivers.add(textFieldTruckNumber);
    textFieldTruckNumber.setColumns(10);

    JTextPane txtpnTrailerNumber = new JTextPane();
    txtpnTrailerNumber.setText("Trailer Number");
    txtpnTrailerNumber.setBounds(5, 185, 140, 15);
    Drivers.add(txtpnTrailerNumber);
    txtpnTrailerNumber.setEditable(false);

    textFieldTrailerNumber = new JTextField();
    textFieldTrailerNumber.setBounds(5, 205, 140, 30);
    Drivers.add(textFieldTrailerNumber);
    textFieldTrailerNumber.setColumns(10);

    JButton btnDeleteDriver = new JButton("Delete Driver");
    btnDeleteDriver.setBounds(359, 185, 117, 29);
    Drivers.add(btnDeleteDriver);

    //New Expenses Panel
    JPanel NewExpense = new JPanel();
    tabbedPane.addTab("New Expense", null, NewExpense, null);
    //adds types of expenses to jcombo
    expenses = new JComboBox(comboboxitems);
    expenses.setBounds(257, 5, 236, 30);
    expenses.setEditable(false);
    expenses.addItemListener(this);
    NewExpense.setLayout(null);
    NewExpense.add(expenses);



    //create panels for each combo option
    JPanel fuel = new JPanel();
    fuel.setBounds(0, 0, 743, 399);
    fuel.setBackground(Color.BLUE);
    JPanel toll = new JPanel();
    toll.setBounds(0, 0, 743, 399);
    toll.setBackground(Color.RED);
    JPanel repair = new JPanel();
    repair.setBounds(0, 0, 743, 399);
    repair.setBackground(Color.BLACK);      

    //Assigns Panels to combo options
    cards = new JPanel(new CardLayout());
    cards.setBounds(5, 29, 743, 399);
    cards.setLayout(null);
    cards.add(fuel, FUEL);
    cards.add(toll, TOLL);
    cards.add(repair, REPAIR);

    NewExpense.add(cards);



    JPanel Income = new JPanel();
    tabbedPane.addTab("Income", null, Income, null);
    Income.setLayout(null);

    JPanel Results = new JPanel();
    tabbedPane.addTab("Result", null, Results, null);
    Results.setLayout(null);

}

@Override
public void itemStateChanged(ItemEvent e) {
            CardLayout cl = (CardLayout)(cards.getLayout());
            cl.show(cards, (String)e.getItem());


}
}

共有1个答案

仰经武
2023-03-14
cards = new JPanel(new CardLayout());
cards.setBounds(5, 29, 743, 399);
cards.setLayout(null); // ???!!!

说真的?您将卡片布局设置为CardLayout,然后立即再次将其设置为NULL。然后,当您尝试使用卡片“布局”时,您会得到一个NullPointerException--这并不奇怪!

 类似资料:
  • 我是python和selenium的新手,我正在尝试在选项卡之间切换,我已经尝试使用普通的键盘命令,ActionChains和Keys.CONTROL,但它不起作用,我将如何做到这一点。程序如下

  • 我有一个带有底栏的main_activity。我正在使用这个底栏在片段之间导航。然而,有一个片段不能通过底部栏直接导航。它必须通过MapView和从片段中访问。然而,每当我使用InfoWindow时,该应用程序就会崩溃。这是我的代码 异常:main process:com.teslaqubitsins.fasih.teslahcm,pid:2573 java.lang.nullpointerexc

  • 我一直在寻找一个解决办法,现在,尽管所有类似的问题和答案,但没有找到任何似乎工作。我想让用户能够通过卡片布局中设置的各种面板进行操作。然而,我希望用来在这些卡片之间切换的按钮在卡片本身,而不是一组单独的按钮,它不会在程序中改变。下面是创建框架的主文件: 编辑:我向MainMenu JPanel的按钮添加了一个自动生成的操作监听器,如下所示: 在主文件(主文件为BattleGraphs_V1)中添加

  • 问题内容: 我在WebDriver中使用Java。我必须在两个框架之间切换。我已经在Selenium IDE中记录了测试用例,并且得到的值是selectFrame relative = top select Frame = Middle Frame 但是有一个问题,它无法识别relative = top和middleFrame。如何使用Java在Selenium WebDriver中解决此问题?

  • 我有这个数组 因为我只想渲染前5张照片。。如何从阵列中获取前5张照片??

  • 当使用底部导航视图在片段之间切换时,每次按下按钮时都会重新创建片段。 这是我的代码: