Java Swing GUI代码更改没有任何区别

时间:2016-08-05 19:11:00

标签: java swing user-interface

我正在使用Swing制作一个扑克游戏的GUI,我正在为按钮编写动作监听器,我最初为SYSOUT" BUtton Listener Works"的按钮创建了一个动作监听器。只是为了检查它是否有效。

我已经更改了这段代码来编写一些正确的侦听器,并且通过一些魔术来渲染仍然是sysouts的GUI ...在下面的代码中,我将侦听按钮删除并添加了一个来调用但是这个更改被忽略了不知何故

我甚至完全删除了动作监听器,保存了所有内容,但仍然写出了这个幻像代码!

此外,ButtonBar中的JComboBox不会显示。

TLDR无论我对GUI的代码做什么,它都会显示旧代码的结果。

请有人救我。

public class ButtonBar extends JPanel {

 public ButtonBar() {
  setBorder(BorderFactory.createTitledBorder("Moves:"));
  JButton checkButton = new JButton("Check");
  JButton callButton = new JButton("Call");
  JButton raiseButton = new JButton("Raise");
  JButton foldButton = new JButton("Fold");
  JLabel selectPlayStyle = new JLabel("Select opponent play style");

  callButton.addActionListener(e -> System.out.println("Sherbert Lemon"));

  //create the comboBox
  String[] playStyleStrings = {
   "Tight Aggressive",
   "Tight Passive",
   "Loose Aggressive",
   "Loose Passive"
  };
  JComboBox < String > playStyleList = new JComboBox < String > (playStyleStrings);
  playStyleList.setSelectedIndex(0);
  playStyleList.addActionListener(e -> System.out.println("none"));

  //DELETE THIS

  setLayout(new GridBagLayout());

  /*this is a class where you can establish where all of your Swing bits and bobs will go.*/
  GridBagConstraints gc = new GridBagConstraints();

  //These show you how much space is allocated to each row
  gc.weightx = 0.2;
  gc.weighty = 0.2;

  //moves button column - all the JLabels
  gc.gridx = 0;
  add(checkButton, gc);

  gc.gridx = 1;
  add(callButton, gc);

  gc.gridx = 2;
  add(raiseButton, gc);

  gc.gridx = 3;
  add(foldButton, gc);

  gc.gridx = 4;
  add(selectPlayStyle, gc);

  gc.gridx = 5;
  add(playStyleList, gc);
 }
}

App

public class App {

 public static void main(String[] args) {
  /*Swing likes to manage it's own thread so this allows it to do so.
  This creates a Swing Thread and runs the method inside of it for you */
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    //main window for the application to load into
    JFrame frame = new MainFrame("Tren Poker Systems");
    frame.setSize(1000, 600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   }
  });
  GameManager gm = new GameManager();
 }
}

MainFrame

public class MainFrame extends JFrame {

 private OddsPanel oddsPanel;
 private CardsPanel cardsPanel;
 private ButtonBar buttonBar;
 private static JTextArea gameInfoArea;

 public MainFrame(String title) {
  super(title);

  //Create Swing components.
  gameInfoArea = new JTextArea("Game information will be found here: ");
  oddsPanel = new OddsPanel();
  cardsPanel = new CardsPanel();
  buttonBar = new ButtonBar();

  //Set layout manager.
  setLayout(new BorderLayout());

  Container container = getContentPane();
  container.add(gameInfoArea, BorderLayout.CENTER);
  container.add(oddsPanel, BorderLayout.WEST);
  container.add(cardsPanel, BorderLayout.EAST);
  container.add(buttonBar, BorderLayout.SOUTH);
 }

 public static void addToGamePane(String information) {
  gameInfoArea.append(information + "\n");
 }
}

0 个答案:

没有答案