Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Java) Why is this tabbed pane not working correctly?
    primarykey
    data
    text
    <p>Note: I've searched all over the web (this site and others) and cannot answer this myself.</p> <p>Ok guys. I am a new Java programmer, and we just got done covering tabbed panes. This is the state in which I turned in my assignment: it doesn't work, and I can't figure out why. I've changed so much crap around, I can't keep it straight in my head anymore, but I know it's probably something incredibly simple.</p> <p>I apologize for the length of the code, but I'm trying to give you the entirety of my code so you can tell me where I jacked it up.</p> <p>Thanks in advance. -- Also, I'm aware there are other Warnings (i.e. unused imports), but I'm not worried about those. And, this will not affect my grade (as I said, already submitted), but I want to know wtf I did wrong!</p> <pre><code>import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.text.DecimalFormat; import java.awt.Color; import javax.swing.JOptionPane; import javax.swing.JTabbedPane; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.JComponent; public class TabbedPane1 extends JPanel { public TabbedPane1() { JTabbedPane tabbedPane = new JTabbedPane(); tabbedPane.addTab("DayGui", new DayGui()); tabbedPane.addTab("OfficeCalc", new OfficeAreaCalculator()); add(tabbedPane); } public static void main(String[] args) { JFrame myFrame = new JFrame("Tabbed Programs"); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.add(new TabbedPane1(), BorderLayout.CENTER); myFrame.pack(); myFrame.setVisible(true); } class DayGui extends JPanel { private JPanel mainFrame; private JButton cmdGood; private JButton cmdBad; public DayGui() { mainFrame = new JPanel(); cmdGood = new JButton("Good"); cmdBad = new JButton("Bad"); Container myContainer = mainFrame; myContainer.setLayout(new FlowLayout()); myContainer.add(cmdGood); myContainer.add(cmdBad); cmdGood.setMnemonic('G'); cmdBad.setMnemonic('B'); mainFrame.setSize(300, 100); myContainer.setBackground(Color.blue); cmdGood.setBackground(Color.cyan); cmdBad.setBackground(Color.cyan); /*mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });*/ ButtonsHandler bhandler = new ButtonsHandler(); cmdGood.addActionListener(bhandler); cmdBad.addActionListener(bhandler); //mainFrame.setVisible(true); } class ButtonsHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == cmdGood) JOptionPane.showMessageDialog(null, "Today is a good day!", "Event Handler Message", JOptionPane.INFORMATION_MESSAGE); if(e.getSource() == cmdBad) JOptionPane.showMessageDialog(null, "Today is a bad day!", "Event Handler Message", JOptionPane.INFORMATION_MESSAGE); } } } class OfficeAreaCalculator extends JPanel { private JPanel mainFrame; private JButton calculateButton; private JButton exitButton; private JTextField lengthField; private JTextField widthField; private JTextField areaField; private JLabel lengthLabel; private JLabel widthLabel; private JLabel areaLabel; public OfficeAreaCalculator() { mainFrame = new JPanel(); exitButton = new JButton("Exit"); calculateButton = new JButton("Calculate"); lengthField = new JTextField(5); widthField = new JTextField(5); lengthLabel = new JLabel("Enter the length of the office:"); widthLabel = new JLabel("Enter the width of the office:"); areaLabel = new JLabel("Office area:"); areaField = new JTextField(5); areaField.setEditable(false); Container c = mainFrame; c.setLayout(new FlowLayout()); c.setBackground(Color.green); c.add(lengthLabel); c.add(lengthField); c.add(widthLabel); c.add(widthField); c.add(areaLabel); c.add(areaField); c.add(calculateButton); c.add(exitButton); calculateButton.setMnemonic('C'); exitButton.setMnemonic('x'); mainFrame.setSize(260, 150); /*mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });*/ CalculateButtonHandler chandler = new CalculateButtonHandler(); calculateButton.addActionListener(chandler); ExitButtonHandler ehandler = new ExitButtonHandler(); exitButton.addActionListener(ehandler); FocusHandler fhandler = new FocusHandler(); lengthField.addFocusListener(fhandler); widthField.addFocusListener(fhandler); areaField.addFocusListener(fhandler); //mainFrame.setVisible(true); } class CalculateButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { DecimalFormat num = new DecimalFormat(",###.##"); double width, length, area; String instring; instring = lengthField.getText(); if(instring.equals("")) { instring = ("0"); lengthField.setText("0"); } length = Double.parseDouble(instring); instring = widthField.getText(); if(instring.equals("")) { instring = ("0"); widthField.setText("0"); } width = Double.parseDouble(instring); area = length * width; areaField.setText(num.format(area)); } } class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } class FocusHandler implements FocusListener { public void focusGained(FocusEvent e) { if(e.getSource() == lengthField || e.getSource() == widthField) { areaField.setText(""); } else if(e.getSource() == areaField) { calculateButton.requestFocus(); } } public void focusLost(FocusEvent e) { if(e.getSource() == widthField) { calculateButton.requestFocus(); } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload