:
Swing / AWT / SWT
Unexpected JButton object
Jackson Steward
Greenhorn
Posts: 9
posted 3 years ago
I am trying to
test
a new program with 2 interfaces "ActionListener" and "ControllerEventListener". What I expect from the program during the execution would be that when I run the program, a GUI appears with a button on it. After I click on the button, the there would be a chain of sound appearing along with the random animation on the GUI. However, there is also an unwanted button popping up at the left corner of the GUI. Why does that button appear and why is it in the left corner? Thanks in advance.
package Self_assigned; import javax.swing.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.*; import javax.sound.midi.*; public class JFrame_MidiEvent implements ActionListener{ static JFrame frame = new JFrame(); JLabel label = new JLabel("Hi there"); JButton button = new JButton("Press here"); MyDrawPanel mp = new MyDrawPanel(); public static void main(String[] args) { JFrame_MidiEvent obj = new JFrame_MidiEvent(); obj.go(); } public void setGui() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(mp); frame.getContentPane().add(BorderLayout.SOUTH, button); frame.getContentPane().add(BorderLayout.NORTH, label); button.addActionListener(this); // frame.setBounds frame.setSize(400, 400); frame.setVisible(true); } public void go() { System.out.println("thit ga"); setGui(); } public MidiEvent makeEvent(int comd,int chan, int one, int two, int tick) { MidiEvent event = null; int a = (int) Math.random(); try { ShortMessage message = new ShortMessage(); message.setMessage(comd, chan, one, two); event = new MidiEvent(message, tick); } catch (InvalidMidiDataException e) { e.printStackTrace(); } return event; } class MyDrawPanel extends JPanel implements ControllerEventListener { boolean condition = false; @Override public void controlChange(ShortMessage message) { condition = true; repaint(); } public void paintComponent(Graphics g) { if (condition) {, width, ht); condition = false; } // close if } } @Override public void actionPerformed(ActionEvent arg0) { try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.addControllerEventListener(mp, new int[]{127}); Sequence seq = new Sequence(Sequence.PPQ, 4); Track track = seq.createTrack(); for (int i = 1; i < 60; i++) { int r = (int) (Math.random() * 70 + 1); track.add(makeEvent(144, 1, r, 60, i)); track.add(makeEvent(176, 1, 127, 0, i)); track.add(makeEvent(128,1, 50, 60, i + 2)); } sequencer.setSequence(seq); sequencer.start(); sequencer.setTempoInBPM(120); } catch (Exception e) { e.printStackTrace(); } } }
Paul Clapham
Marshal
Posts: 25948
69
I like...
posted 3 years ago
So... the default LayoutManager for a JFrame's content pane is BorderLayout. But then you change the content pane to be a JPanel, whose default LayoutManager is FlowLayout.
(I believe that's correct; I've always explicitly set the LayoutManager I want so I've never had to rely on the above statement.)
And you're adding components to that content pane as if its LayoutManager were a BorderLayout. But suppose my understanding is correct. Then you're using a FlowLayout as if it were a BorderLayout and things are likely to go awry. And since things are going awry, in particular since your JFrame isn't being laid out as you think it should, that suggests I could well be right.
Maneesh Godbole
Bartender
Posts: 11445
19
I like...
posted 3 years ago
Paul is indeed right. The content pane defaults to BorderLayout whereas the JPanel defaults to FlowLayout.
Solution: Define your panel to have BorderLayout either via an explicit call or inside a constructor
[
How to ask questions
] [
Donate a pint, save a life!
] [
Onff-turn it on!
]
Jackson Steward
Greenhorn
Posts: 9
posted 3 years ago
Can someone explains to me what is the difference between BorderLayout and FlowLayout. I've just recently started learning how to work with Swing and still have a lot of things that I don't understand. How should I fix my code then?
Campbell Ritchie
Marshal
Posts: 70636
288
posted 3 years ago
You should be able to find out all you need to know about layouts (except how to simplify grid bag constraints) in the
Java™ Tutorials
. Flow is like line wrapping in a text editor when you alter its width. Border has up to five areas which contain other components and they all can change size but don't change their relative positions.
Norman Radder
Ranch Hand
Posts: 146
4
posted 3 years ago
It's also helpful sometimes to read the API
doc
for the classes to see what it says:
Jackson Steward
Greenhorn
Posts: 9
posted 3 years ago
Thanks for your response. I am still trying to improve my skill and to be able to understand all the issues explained in the
java
doc is still somewhat challenging to me. I'll make sure that I'll get the hang of it.
Paul Clapham
Marshal
Posts: 25948
69
I like...
posted 3 years ago
The analogy I like to give is this: when you want to learn how to juggle flaming chainsaws, there are three steps:
1. Learn to juggle.
2. Learn to juggle chainsaws.
3. Learn to juggle flaming chainsaws.
Skipping earlier steps in the process can be hazardous. But Swing isn't really like that, you really have to learn a whole lot of things all at once so you don't have unexpected consequences. Working your way through the tutorials is definitely recommended. Although admittedly having a JButton show up in the wrong place is less serious than having a flaming chainsaw show up in the wrong place!
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Music Player
MiniMusicPlayer3.java from Head First Java - Sounds like drums to me.
widget not loading in frame
Head First Java, Ch12, MiniMusicPlayer3 not working as advertised
Head First Java problem
More...