I am making my first program in Java, which would be a simple text editor..
Well, I have a JPanel and I want to add a JTextArea in it..But the JTextArea fits all the JPanel,so I cant add anything else like a Button in my JPanel..
I have tried to resize my JTextArea but nothing happens..
The code I have used:
TextArea text = new TextArea();
text.setSize(300, 200);
JFrame.add(text);
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
final JTextArea area = new JTextArea(2,20);
frame.getContentPane().add(area);
JButton button1 = new JButton("press me");
JButton button2 = new JButton("press me");
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Related
I am trying to make two buttons in JPanel side by side. I have the following code as of now:
JFrame frame = new JFrame("Display Shapes");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JButton button = new JButton();
button.setBounds(50,100,80,30);
button.setText("Load Shapes");
panel.add(button);
JButton button2 = new JButton();
button2.setBounds(100,100,80,30);
button.setText("Sort Shapes");
panel.add(button2);
frame.add(panel);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
When I run the code, below is the output I see. I am not sure why the second button is not appearing correctly. What could be the reason for this?
The place where you are writing button.setText("Sort Shapes");, it will be button2.setText("Sort Shapes");
It is changing the text of the first button whose text had been set earlier
I am using Netbeans to create a Java desktop application. I have created two different JPanels. In one I have inserted a button, and in the other just some settings. How can I connect the JButton, in the other JPanel, to change the settings of the other one? What code should I use? (Keep in mind that I am a beginner.)
A very simple programm. It dosen't matter if the button and the label are in the same JPanel or not.
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 300, 300);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 150));
JLabel label = new JLabel("Test string");
JButton button = new JButton("Push me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
label.setText("change text");
}
});
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
I'm trying to create a JPanel (non-resizable) showing a grid of buttons but when I try to add the JPanel to a JFrame it won't show.
JFrame frame = new JFrame("frame");
JPanel panel = new JPanel();
frame.setSize(681,920);
frame.setResizable(true);
JLabel label = new JLabel();
label.setLayout(new FlowLayout(FlowLayout.LEADING,0,0));
JButton btn = new JButton();
btn.setContentAreaFilled( false );
btn.setBorder( null );
btn.setBounds(214,210,0,0);
label.add(btn);
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
The output should be a resizable frame with inside a 3x4 grid of buttons.
If I don't use the panel and I put the line frame.setResizable(false) it works as expected but I need to add more stuff to the frame so I need to put the buttons safe in a panel.
Both panel and label are added to your frame, to make sure they are added write
JLabel label = new JLabel("JLABEL");
and
panel.setBackground(Color.BLUE);
im starting to work with jpanel, but after a lot of reading and going crazy, i couldnt understand how to make it work...
so im trying to open a frame with 2 buttons, i was able to do that, but i cant postion them in the frame, here is my code:
JFrame frame = new JFrame();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton but = new JButton("text");
but.setLocation(20, 20);
JButton but2 = new JButton("list");
but2.setLocation(50, 50);
JPanel panel= new JPanel();
panel.add(but);
panel.add(but2);
frame.add(panel);
frame.setVisible(true);
How do I connect a frame to an existing frame?
The code below is the code for the appletframe. What I want to do is add the other code which is for the frame to be connected to the bottom of the AppletFrame, so that when I drag the Appletframe the frame code with we dragged with it as well. Basically I want the frame code to be attached with the appletFrame so that both the frames are together.
AppletFrame
appletFrame = new JFrame(Settings.serverName);
Loader.webclient = false;
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
appletPanel.setLayout(new BorderLayout());
appletFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/icon.png")));
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, "Center");
appletFrame.pack();
appletFrame.setLocationRelativeTo(null);
appletFrame.setVisible(true);
JMenuBar jmenubar = new JMenuBar();
appletFrame.setJMenuBar(jmenubar);
Layout = new FlowLayout();
ImageIcon keyboard = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/keyboard.png")));
ImageIcon wrench = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/resources/wrench.png")));
Button1 = new JButton("Vote");
Button2 = new JButton("Item List");
Button3 = new JButton("Screenshot");
Button4 = new JButton(wrench);
Button5 = new JButton(keyboard);
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(Layout);
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
Button1.addActionListener(this);
Button2.addActionListener(this);
Button3.addActionListener(this);
Button4.addActionListener(this);
Button5.addActionListener(this);
Button1.setText("Vote");
Button2.setText("Item List");
Button3.setText("Screenshot");
Frame which I want it to be attached with the AppletFrame. I want this to be attached to the bottom of the appletFrame, but I don't know how to do it.
JFrame frame = new JFrame();
frame.setSize(775,121);
frame.setResizable(false);
JTextArea textArea = new JTextArea("TEST");
textArea.setSize(400,400);
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (textArea);
scroll.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scroll);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
As I alluded to in my first comment, this GUI would be better combined into a single top-level container.
Here is an SSCCE1 (mentioned in my 2nd comment) that shows the basic idea, though now I have a better idea of the effect required, the JSplitPane seems less appropriate. Here I just combine the GUI elements into the same layout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGUI extends JPanel {
TestGUI() {
JFrame appletFrame = new JFrame("Settings.serverName");
appletFrame.setLayout(new BorderLayout());
appletFrame.setDefaultCloseOperation(3);
JPanel appletPanel = new JPanel(new BorderLayout());
appletPanel.add(this);
appletPanel.setPreferredSize(new Dimension(767, 537));
appletFrame.getContentPane().add(appletPanel, BorderLayout.CENTER);
// Don't use a menu-bar as a tool-bar!
JToolBar jmenubar = new JToolBar();
appletPanel.add(jmenubar, BorderLayout.PAGE_START);
JButton Button1 = new JButton("Vote");
JButton Button2 = new JButton("Item List");
JButton Button3 = new JButton("Screenshot");
JButton Button4 = new JButton("wrench");
JButton Button5 = new JButton("keyboard");
Button4.setBorder(null);
Button4.setBorderPainted(false);
Button4.setContentAreaFilled(false);
Button5.setBorder(null);
Button5.setBorderPainted(false);
Button5.setContentAreaFilled(false);
jmenubar.setLayout(new FlowLayout());
jmenubar.add(Button1);
jmenubar.add(Button2);
jmenubar.add(Button3);
jmenubar.add(Button4);
jmenubar.add(Button5);
JTextArea textArea = new JTextArea("TEST", 4, 65 );
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);
JScrollPane scroll = new JScrollPane (
textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
appletPanel.add(scroll, BorderLayout.PAGE_END);
appletFrame.pack();
appletFrame.setLocationByPlatform(true);
appletFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
new TestGUI();
}
});
}
}
And yes, this would have arrived sooner if I'd had an SSCCE to start with. ;)