Resolved jbutton problems between Windows/Mac

Discussion in 'Mac Programming' started by canada eh, Sep 15, 2011.

  1. canada eh, Sep 15, 2011
    Last edited: Sep 15, 2011

    canada eh macrumors regular

    Joined:
    Dec 22, 2009
    Location:
    Barrie, Ontario
    #1
    I am currently in a high school programming class and we just started learning about jbutton in GUI applications. At school we use Jcreator 5.0 as our IDE but I use Netbeans 7.0 on a Mac. The problem I am having is that when I run the code he has given us to do an assignment the button itself doesn’t show up, only the text does. I can click the button with no problems there is just no borders. Here is the code. Thanks :D

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GUIbutton implements ActionListener {
        // declare objects in window
    
        JFrame frame;
        JPanel contentPane;
        JLabel label1;
        JLabel label2;
        JButton button1;
        JButton button2;
    
        public GUIbutton() {    // this constructs the GUI object and
    
    
            // Create and set up the frame 
            frame = new JFrame("JButtons on a GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // the box layout puts objects in a vertical column 
            contentPane = new JPanel();
            contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
            contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            contentPane.setBackground(Color.red);
    
            // Add content pane to frame 
            frame.setContentPane(contentPane);
    
    
            // Create and add label1 
            label1 = new JLabel("Good Morning ICS4U !");
            label1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            label1.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            label1.setForeground(Color.white);
            contentPane.add(label1);
    
            // create button1
            button1 = new JButton("English");
            button1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            button1.setAlignmentX(JButton.CENTER_ALIGNMENT);
            button1.setBackground(Color.blue);
            button1.addActionListener(this);
            contentPane.add(button1);
    
            // create button2
            
            button2 = new JButton("French");
            button2.setBackground(Color.white);
            button2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            button2.setAlignmentX(JButton.CENTER_ALIGNMENT);
            //button2.setBackground(Color.white);
            button2.addActionListener(this);
            contentPane.add(button2);
    
            // create label2
            label2 = new JLabel("Java Applications can run on any platform.");
            label2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            label2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
            label2.setForeground(Color.white);
            contentPane.add(label2);
    
            // Size and then display the window. 
            frame.setSize(500, 300);
            frame.setLocation(300, 300);
            frame.setVisible(true);
    
        }
    
        public void actionPerformed(ActionEvent event) {
    
            String eventName = event.getActionCommand();
    
            // check to see which button is clicked
    
            if (eventName.equals("French")) {
                label1.setText("Bonjour ICS4U !");
                label2.setText("Les applications Java peuvent fonctionner sur n'importe quelle plate-forme. ");
            }
    
            if (eventName.equals("English")) {
                label1.setText("Good Morning ICS4U !");
                label2.setText("Java Applications can run on any platform.");
    
            }
    
        }
    
        public static void main(String[] args) {
    
            // create  a GUI called g1 and run !
            GUIbutton g1 = new GUIbutton();
    
        }
    }
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Remove the borders being assigned to the JButtons with setBorder().

    JButtons on Mac OS X are not infinitely customizable. If you add a border, it will no longer be drawn as a typical pushbutton. You also can't change the JButton's colors arbitrarily.

    For more about customizing JButtons on Mac OS X, google search terms:
    mac os java jbutton
     
  3. canada eh thread starter macrumors regular

    Joined:
    Dec 22, 2009
    Location:
    Barrie, Ontario
    #3
    That worked perfectly!

    I did notice that the border colouring I tried to do didn't work, thats what my teacher recommended I try.

    Thanks a lot! :D
     

Share This Page