Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ul> <li>A quick review of your code, brought your code down to this length. Imagine, what you can achieve if you give it a bit more insight.</li> <li>Furthermore, when you see that in your code, you are repeating some lines again and again for the same thingy, you can indeed think of a pattern that can accomplish that for you in a rightful manner, with fewer keystrokes.</li> </ul> <p>Try this modified code of yours :-)</p> <pre><code>import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.*; public class Filecabinet extends JFrame implements ActionListener { private String folder = ""; //create buttons private JButton[] button = new JButton[26]; //create panels private JPanel aF = new JPanel(); private JPanel gL = new JPanel(); private JPanel mR = new JPanel(); private JPanel sX = new JPanel(); private JPanel yZ = new JPanel(); private JPanel readout = new JPanel(); private JLabel notify = new JLabel("You have selected folder " + folder); public void ComposePane(){ init_Buttons(button); //set buttons to panels for (int i = 0; i &lt; 6; i++) aF.add(button[i]); //set layout of panels aF.setLayout(new GridLayout(2,3)); for (int i = 6; i &lt; 12; i++) gL.add(button[i]); gL.setLayout(new GridLayout(2,3)); for (int i = 12; i &lt; 18; i++) mR.add(button[i]); mR.setLayout(new GridLayout(2,3)); for (int i = 18; i &lt; 24; i++) sX.add(button[i]); sX.setLayout(new GridLayout(2,3)); for (int i = 24; i &lt; 26; i++) yZ.add(button[i]); yZ.add(readout); readout.add(notify); yZ.setLayout(new GridLayout(2,3)); } private void init_Buttons(JButton[] button) { int value = 65; for (int i = 0; i &lt; button.length; i++) { button[i] = new JButton(Character.toString((char) value++)); button[i].addActionListener(this); } } public Filecabinet(){ setTitle("File Cabinet"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(5,1)); ComposePane(); add(aF); add(gL); add(mR); add(sX); add(yZ); } public void actionPerformed(ActionEvent e) { folder = ((JButton) e.getSource()).getText(); notify.setText(folder); } public static void main(String[]args){ SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Filecabinet frame = new Filecabinet(); final int WIDTH = 600; final int HEIGHT = 600; //frame.setSize(WIDTH, HEIGHT); frame.pack(); frame.setVisible(true); } }); } } </code></pre>
 

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