Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javac JSwingExample.java && jar cfe JSwingExample.jar JSwingExample JSwingExample.class && java -jar JSwingExample.jar
- https://docs.oracle.com/javase/8/docs/api/javax/swing/JMenuItem.html
- */
- import javax.swing.*;
- import javax.swing.border.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class JSwingExample extends JFrame implements ActionListener {
- JMenuBar jmb;
- JMenu jmF;
- JMenuItem jmiO;
- JMenuItem jmiS;
- JMenuItem jmiQ;
- JMenu jmT;
- JMenuItem jmi1;
- JMenuItem jmi2;
- JMenuItem jmi3;
- //JMenuBar statusBar;
- JPanel statusBar;
- JLabel statusLabel;
- JTextArea ta;
- JTextArea ta2;
- public static void main(String args[]){
- new JSwingExample();
- }
- JSwingExample(){
- jmb = new JMenuBar();
- jmF = new JMenu("File");
- jmiO = new JMenuItem("Open"); jmiO.addActionListener(this); jmF.add(jmiO);
- jmiS = new JMenuItem("Save"); jmiS.addActionListener(this); jmF.add(jmiS);
- jmiS.setEnabled(false);
- jmF.addSeparator();
- jmiQ = new JMenuItem("Quit"); jmiQ.addActionListener(this); jmF.add(jmiQ);
- jmb.add(jmF);
- jmb.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.lightGray));
- jmT = new JMenu("Tools");
- jmi1 = new JMenuItem("1"); jmi1.addActionListener(this); jmT.add(jmi1);
- jmi2 = new JMenuItem("2"); jmi2.addActionListener(this); jmT.add(jmi2);
- jmi3 = new JMenuItem("3"); jmi3.addActionListener(this); jmT.add(jmi3);
- jmb.add(jmT);
- ta=new JTextArea("...\n");
- ta.setLineWrap(true);
- ta.setWrapStyleWord(true);
- ta2=new JTextArea("...\n");
- //ta.setMinimumSize(100);
- JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
- splitPane.setLeftComponent(ta);
- //splitPane.getLeftComponent().setMinimumSize(100);
- splitPane.setRightComponent(ta2);
- splitPane.setOneTouchExpandable(true);
- splitPane.setDividerLocation(150);
- splitPane.setDividerLocation(0.5);
- add(splitPane,BorderLayout.CENTER);
- //statusBar = new JMenuBar();
- statusBar = new JPanel();
- statusLabel=new JLabel("...");
- //statusLabel.setBounds(50,50,100,30);
- statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
- statusLabel.setFont(new Font("sansserif", Font.PLAIN, 11));
- statusBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
- statusBar.setPreferredSize(new Dimension(getWidth(), 20 ));
- statusBar.setLayout(new BoxLayout(statusBar, BoxLayout.X_AXIS));
- statusBar.add(statusLabel);
- add(statusBar,BorderLayout.SOUTH);
- setJMenuBar(jmb);
- setTitle(System.getProperty("user.name"));
- setSize(600,400);
- setLocationRelativeTo(null);
- setVisible(true);
- setDefaultCloseOperation(this.EXIT_ON_CLOSE);
- revalidate();
- }
- public void actionPerformed(ActionEvent e){
- ta.append(e.getSource().toString()+"\n");
- statusLabel.setText(e.getActionCommand());
- if ( e.getActionCommand().equals("Quit") ) { System.exit(0); }
- if ( e.getActionCommand() == "Open" ) {
- jmiS.setEnabled(true);
- JFileChooser fileChooser = new JFileChooser();
- //fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
- fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
- if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
- File selectedFile = fileChooser.getSelectedFile();
- ta.append("Selected file: " + selectedFile.getAbsolutePath());
- }
- }
- //JButton btn = (JButton) e.getSource();
- //String pressedKey = (String) btn.getClientProperty("key");
- /*if (CAPS.equals(pressedKey)) {
- for (JButton button : buttons) {
- String text = button.getText();
- text = caps ? text.toLowerCase() : text.toUpperCase();
- button.setText(text);
- }
- caps = !caps;
- return;
- }*/
- //statusLabel.setText(statusLabel.getText()+" "+pressedKey);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement