Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.GridLayout;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import javax.swing.*;
- public class MainGUI2 {
- private static final int ROWS = 24;
- private static final int COLS = 48;
- private static final Insets LIST_MARGINS = new Insets(2, 4, 0, 0);
- private JPanel mainPanel = new JPanel();
- private JTextArea listText = new JTextArea(ROWS, COLS);
- public MainGUI2() {
- listText.setEditable(false);
- listText.setFocusable(false);
- listText.setWrapStyleWord(true);
- listText.setLineWrap(true);
- listText.setMargin(LIST_MARGINS);
- JScrollPane scrollPane = new JScrollPane(listText);
- scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- JPanel buttonPanel = new JPanel();
- buttonPanel.setLayout(new GridLayout(0, 1, 0, 1));
- buttonPanel.add(new JButton(new RefreshAction("Refresh", this)));
- JPanel eastPanel = new JPanel(new BorderLayout());
- eastPanel.add(buttonPanel, BorderLayout.PAGE_START);
- eastPanel.setBackground(Color.DARK_GRAY);
- mainPanel.setLayout(new BorderLayout());
- mainPanel.add(scrollPane, BorderLayout.CENTER);
- mainPanel.add(eastPanel, BorderLayout.LINE_END);
- }
- public JPanel getMainPanel() {
- return mainPanel;
- }
- public void appendText(String text) {
- listText.append(text);
- }
- private static void createAndShowGui() {
- MainGUI2 mainGui = new MainGUI2();
- JFrame frame = new JFrame("GUI");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().add(mainGui.getMainPanel());
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> createAndShowGui());
- }
- }
- @SuppressWarnings("serial")
- class RefreshAction extends AbstractAction {
- private MainGUI2 main;
- public RefreshAction(String name, MainGUI2 main) {
- super(name);
- int mnemonic = (int) name.charAt(0);
- putValue(MNEMONIC_KEY, mnemonic);
- this.main = main;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- main.appendText("From Refresh Action!\n");
- }
- }
Add Comment
Please, Sign In to add comment