Java Swing GUI显示正常但功能失效,请求故障排查与修复
Fixing Your Java GUI Calculation Program Issues
Hey there! Let's figure out why your Java GUI calculator isn't working and fix it up. I've gone through your code and spotted a few key issues that are causing the problems, plus some improvements we can make for better functionality and user experience.
Key Problems in Your Original Code
- Broken Event Handling Logic: The biggest issue is in your
actionPerformedmethod. You first check if the event came from thezgjidhradio button, then immediately check if it's theplus/minus/fshibuttons. That's impossible—an event can only come from one component at a time! So when you click theplusbutton, the code never enters the calculation block. - No Input Validation: If the user leaves input fields empty or enters non-numeric text, calling
Double.parseDouble()will throw aNumberFormatExceptionand crash your program. - Unlinked Radio Button & Buttons: Right now, there's no connection between selecting
zgjidhand being able to use the calculation buttons. Users can click them even ifzgjidhisn't selected, which doesn't match your requirements.
Fixed & Improved Code
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Gui extends JFrame implements ActionListener { JButton plus, minus, fshi; JTextField vn1, vn2, vrez; JLabel n1, n2, rezultati; JRadioButton zgjidh, dil; ButtonGroup grupi; public Gui() { setLayout(new BorderLayout()); // Explicitly set layout for clarity JPanel veri = new JPanel(); veri.setLayout(new GridLayout(3, 2)); n1 = new JLabel("Jep numrin e 1"); veri.add(n1); vn1 = new JTextField(10); veri.add(vn1); n2 = new JLabel("Jep numrin e 2"); veri.add(n2); vn2 = new JTextField(10); veri.add(vn2); rezultati = new JLabel("Rezultati"); veri.add(rezultati); vrez = new JTextField(10); vrez.setEditable(false); // Prevent user from editing result field veri.add(vrez); add(veri, BorderLayout.NORTH); JPanel qender = new JPanel(); qender.setLayout(new GridLayout(2, 1)); zgjidh = new JRadioButton("Zgidh Veprimin"); zgjidh.addActionListener(this); dil = new JRadioButton("Dil"); dil.addActionListener(this); grupi = new ButtonGroup(); grupi.add(zgjidh); grupi.add(dil); qender.add(zgjidh); qender.add(dil); add(qender, BorderLayout.CENTER); JPanel jug = new JPanel(); jug.setLayout(new GridLayout(1, 3)); plus = new JButton("+"); plus.addActionListener(this); plus.setEnabled(false); // Disable until zgjidh is selected jug.add(plus); minus = new JButton("-"); minus.addActionListener(this); minus.setEnabled(false); jug.add(minus); fshi = new JButton("Fshij"); fshi.addActionListener(this); fshi.setEnabled(false); jug.add(fshi); add(jug, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { // Handle radio button selections first if (e.getSource() == zgjidh) { // Enable calculation and clear buttons when zgjidh is chosen plus.setEnabled(true); minus.setEnabled(true); fshi.setEnabled(true); } else if (e.getSource() == dil) { // Close the application when Dil is selected System.exit(0); } // Handle calculation and clear button clicks else if (e.getSource() == plus) { try { double num1 = Double.parseDouble(vn1.getText()); double num2 = Double.parseDouble(vn2.getText()); vrez.setText(String.valueOf(num1 + num2)); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "Ju lutem jepni numra valid!", "Gabim", JOptionPane.ERROR_MESSAGE); } } else if (e.getSource() == minus) { try { double num1 = Double.parseDouble(vn1.getText()); double num2 = Double.parseDouble(vn2.getText()); vrez.setText(String.valueOf(num1 - num2)); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "Ju lutem jepni numra valid!", "Gabim", JOptionPane.ERROR_MESSAGE); } } else if (e.getSource() == fshi) { // Clear all input fields vn1.setText(""); vn2.setText(""); vrez.setText(""); } } public static void main(String args[]) { Gui ob = new Gui(); ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ob.setSize(500, 300); // Adjusted size for better layout ob.setTitle("Kalkulator"); // Added window title ob.setVisible(true); } }
What We Changed & Why
- Fixed Event Flow: We separated radio button logic from button click logic. Now selecting
zgjidhenables the calculation/clear buttons, and clicking those buttons triggers the correct actions without conflicting checks. - Added Input Safety: Wrapped the number parsing in a try-catch block to catch invalid input. This shows a friendly error message instead of crashing the program.
- Improved User Experience:
- Made the result field non-editable so users can't accidentally change it
- Disabled calculation buttons by default (they only activate when
zgjidhis selected, matching your requirement) - Adjusted the window size for a cleaner layout
- Added a window title for clarity
- Cleaned Up Layout: Explicitly set the JFrame's layout to BorderLayout (though it's the default, this makes the code easier to read for anyone else looking at it).
内容的提问来源于stack exchange,提问作者Sulejman Latifaj
相关产品推荐
相关产品推荐

