I want to make the tag (이름: 홍길동 학번: 1111111) looks like this,
이름:
홍길동
학번:
111111
but only I can make looks like this,
이름: 홍길동 학번: 111111
I made it to JLabel on SidePanel which extends JPanel. And \n is not working on JPanel I guess? ..and I don't know how to fix it.
Do I need to make some other JPanel on the SidePanel or use another Layout?? like.. Grid or null? or more JLabel??
Here's my code.
public class MyFrame extends JFrame {
private JButton proscons = new JButton();
private JLabel tag = new JLabel();
private JLabel num = new JLabel();
MyFrame() {
setTitle("융프2 기말고사");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(new WestPanel(), BorderLayout.WEST);
cp.add(new MyPanel(), BorderLayout.CENTER);
setLocationRelativeTo(null); // 가운데서 GUI 창 뜨도록
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class WestPanel extends JPanel {
WestPanel() {
setBackground(Color.YELLOW);
setSize(100,400);
add(proscons);
proscons.setText("찬성");
add(tag);
tag.setText("이름: \n홍길동");
add(num);
num.setText("학번: \n11111111");
}
}
class MyPanel extends JPanel {
MyPanel() {
setBackground(Color.lightGray);
}
}
public static void main(String[] args) {
new MyFrame();
}
}
You can use HTML text formatting in JLabels.
Try doing this:
tag.setText("<html>이름:<br>홍길동</html>");
num.setText("<html>학번:<br>11111111</html>");
Related
I'm using a JPanel and tring to create a two new panels programatically inside this JPanel
public class MainWindow extends javax.swing.JFrame {
/**
* Creates new form MainWindow
*/
private javax.swing.JPanel jviewer;
public MainWindow() {
initComponents();
jviewer = new ImageRender(123);
}
}
For that reason, I have the next extension:
public class ImageRender extends JPanel {
JPanel mainViewer = new JPanel();
JPanel galleryViewer = new JPanel();
public ImageRender(Integer itemnum) {
setLayout(null);
mainViewer = new JPanel();
mainViewer.setBackground(Color.red);
mainViewer.setBounds(0, 0, 200, 200);
galleryViewer = new JPanel();
galleryViewer.setBackground(Color.green);
galleryViewer.setBounds(210, 0, 50, 200);
this.add(mainViewer);
add(galleryViewer);
mainViewer.setVisible(true);
setVisible(true);
System.out.println("Se ha finalizado esta tarea");
}
}
But, at this point, is not displaying any of the JPanel created in the ImageRender.java also any errors.
Someone has an idea about how to fix my implementation?
Reason of that is that you create an ImageRender instance, but it is never being added to the JFrame. Use add method in order to achieve that. Also do not use setLayout(null) and setBounds. Use a layout manager instead. Components will be validated automatically, plus you have a resizable window.
The fact ImageRender extends JPanel means that an ImageRender object is also a JPanel, hence it can be added to the frame. (Already mentioned in comments)
Take a look at an example (assume ImageRenderer class is in another file):
public class Test extends JFrame {
public Test() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new ImageRenderer()); //Create and add a new ImageRendere panel
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test().setVisible(true));
}
private static class ImageRenderer extends JPanel {
public ImageRenderer() {
super(new GridLayout());
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBackground(Color.green);
add(leftPanel);
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setBackground(Color.blue);
add(rightPanel);
}
}
}
For some reason I can't get the BorderLayout to set the way it's supposed to. Just would like to know where I'm going wrong.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColorFactory extends JFrame
{
final int width = 500;
final int height = 300;
private JPanel buttonPanel;
private JPanel radioButtonPanel;
private JLabel msgChangeColor;
public ColorFactory()
{
setTitle("Color Factory");
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
createTopPanel();
add(buttonPanel, BorderLayout.NORTH);
createBottomPanel();
add(radioButtonPanel, BorderLayout.SOUTH);
msgChangeColor = new JLabel("Top buttons change the panel color and bottom radio buttons change the text color.");
add(msgChangeColor, BorderLayout.CENTER);
pack();
}
private void createTopPanel()
{
buttonPanel = new JPanel();
setLayout(new FlowLayout());
JButton redButton = new JButton("Red");
redButton.setBackground(Color.RED);
redButton.addActionListener(new ButtonListener());
redButton.setActionCommand("R");
JButton orangeButton = new JButton("Orange");
orangeButton.setBackground(Color.ORANGE);
orangeButton.addActionListener(new ButtonListener());
orangeButton.setActionCommand("O");
JButton yellowButton = new JButton("Yellow");
yellowButton.setBackground(Color.YELLOW);
yellowButton.addActionListener(new ButtonListener());
yellowButton.setActionCommand("Y");
buttonPanel.add(redButton);
buttonPanel.add(orangeButton);
buttonPanel.add(yellowButton);
}
private void createBottomPanel()
{
radioButtonPanel = new JPanel();
setLayout(new FlowLayout());
JRadioButton greenRadioButton = new JRadioButton("Green");
greenRadioButton.setBackground(Color.GREEN);
greenRadioButton.addActionListener(new RadioButtonListener());
greenRadioButton.setActionCommand("G");
JButton blueRadioButton = new JButton("Blue");
blueRadioButton.setBackground(Color.BLUE);
blueRadioButton.addActionListener(new RadioButtonListener());
blueRadioButton.setActionCommand("B");
JButton cyanRadioButton = new JButton("Cyan");
cyanRadioButton.setBackground(Color.CYAN);
cyanRadioButton.addActionListener(new RadioButtonListener());
cyanRadioButton.setActionCommand("C");
radioButtonPanel.add(greenRadioButton);
radioButtonPanel.add(blueRadioButton);
radioButtonPanel.add(cyanRadioButton);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionColor = e.getActionCommand();
if(actionColor.equals("R"))
{
buttonPanel.setBackground(Color.RED);
radioButtonPanel.setBackground(Color.RED);
}
if(actionColor.equals("O"))
{
buttonPanel.setBackground(Color.ORANGE);
radioButtonPanel.setBackground(Color.ORANGE);
}
if(actionColor.equals("Y"))
{
buttonPanel.setBackground(Color.YELLOW);
radioButtonPanel.setBackground(Color.YELLOW);
}
}
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String actionTextColor = e.getActionCommand();
if(actionTextColor.equals("G"))
{
msgChangeColor.setForeground(Color.GREEN);
}
if(actionTextColor.equals("B"))
{
msgChangeColor.setForeground(Color.BLUE);
}
if(actionTextColor.equals("C"))
{
msgChangeColor.setForeground(Color.CYAN);
}
}
}
public static void main(String[] args)
{
ColorFactory run = new ColorFactory();
run.setVisible(true);
}
}
The problem is you are changing the layout manager for the frame when you create your top and bottom panels...
private void createTopPanel() {
buttonPanel = new JPanel();
setLayout(new FlowLayout()); // <--- This is call setLayout on the frame
This is why it's dangerous to...
Extend from something like JFrame directly...
Dynamically build components
It's all to easy to lose context and start effecting components you didn't actually want to...
Another problem (besides the one posted by MadProgrammer) is that you add your components to the JFrame itself.
You should add content to the content pane of the frame which you can get by calling JFrame.getContentPane().
Example:
JFrame f = new JFrame("Test");
Container c = f.getContentPane();
c.add(new JButton("In Center"), BorderLayout.CENTER);
c.add(new JButton("At the Bottom"), BorderLayout.SOUTH);
c.add(new JButton("At the Top"), BorderLayout.NORTH);
c.add(new JButton("On the Left"), BorderLayout.WEST);
c.add(new JButton("On the Right"), BorderLayout.EAST);
You can set/change the content panel by calling JFrame.setContentPane(). The default content panel already has BorderLayout so you don't even need to change it nor to set a new panel.
I am new on Java.
I have developed an application with some different JPanels (using a BorderLayout, 3 panels in this case).
In panel 1, I have a JLabel and a variable (a class) that is related with its value (method get);
in panel 2, I updated the value of the variable (method set) because it is done when an action is performed in this second panel.
How Could I get the value of the JLabel in panel 1 updated?
I don't know how to trigger an event or something similar after updating the value from panel 2 and how to make panel 1 to listen to this change.
Let me explain a bit more. I have a JFrame with two JPanels and I update the model from one panel. Once the model is updated, the JLabel from the other JPanel should be updated:
Main: JFrame
public class MainClass extends JFrame
{
public MainClass()
{
// JPanel 1
....
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
setTitle("Test");
setResizable(false);
setVisible(true);
// JPanel 1
this.add(west, BorderLayout.WEST);
// JPanel 2
this.add(board, BorderLayout.CENTER);
}
public static void main(String[] args)
{
// put your code here
new MainClass ();
}
}
JPanel 1
public class West extends JPanel
{
contFase = new Contador(titulo, valor);
JLabel lblTitulo;
...
lblTitulo.setText = contFase.getText();
this.add(lblTitulo);
...
}
JPanel 2
public class Board extends JPanel implements ActionListener
{
....
public void actionPerformed(ActionEvent e)
{
...
//Here Label of panel 1 should be updated with the model
contFase.setValor(contFase.getValor() + pacman.comerElemento(fase.getPacdots(), fase.getPowerPellets()));
...
}
}
I have little idea how your code looks like because you didn't show any, but here is an example of how to edit a JLabel when an action is taken (in this case - pressing a button). The layout of the components on panels does not matter, but I put 2 panels like you wanted.
public class ValueUpdate extends JFrame {
int x = 0;
final JLabel label = new JLabel(String.valueOf(x));
ValueUpdate() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.add(label);
JButton btn = new JButton("Increment");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x++;
label.setText(String.valueOf(x));
}
});
panel2.add(btn);
getContentPane().add(panel1, BorderLayout.CENTER);
getContentPane().add(panel2, BorderLayout.PAGE_END);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
new ValueUpdate();
}
}
public class Benim extends JFrame {
Container contentArea = getContentPane ();
public Benim(){
JFrame frame=new JFrame("Concentration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
setSize(800, 800);
JButton start=new JButton("Start");
JPanel pane=new JPanel();
pane.add(start);
setVisible(true);
frame.add(start);
frame.add(pane);
/* setContentPane(Container)
JRootPane createRootPane()*/
}
public static void main (String []args){
new Benim();
}
}
My code is that. I tried adding to panel first then adding panel to frame, adding to frame directly. Adding a rootpane but still my button doesnot appear. I am trying to learn for 2 days but i am still at same point.
The instance of JFrame that is shown does not have the JButton added.
Instead invoke setVisible on the JFrame directly
You almost never want to extend JFrame as no new functionality is added
Other points to note
Call setVisible after components have been added
setSize is unnecessary - let pack determine container size
This is the result
public class Benim extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Concentration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton start = new JButton("Start");
JPanel pane = new JPanel();
pane.add(start);
pane.add(start);
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
});
}
}
Why another instance of JFrame? You are extending it, so just call super().
public class Benim extends JFrame {
Container contentArea = getContentPane ();
public Benim(){
super("Concentration");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setSize(800, 800);
JButton start=new JButton("Start");
JPanel pane=new JPanel();
pane.add(start);
add(pane);
setVisible(true);
}
public static void main (String []args){
new Benim();
}
}
Reimeus also rightfully points out that you don't need to extend JFrame if you don't plan on extending functionality. See his example for an alternative implementation.
I dont know how to init the JFrame windows. What I need to write to init im?
I have created at the main this:
Panel Panel=new Panel();
Panel.init();
JFrame frame = new JFrame("Shape Project");
frame.add(Panel);
frame.setResizable(false);
frame.setSize(new Dimension(1200, 650));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
and in the JPanel class I have write this:
public class Panel extends JFrame
{
public void init()
{
}
}
But when I active the frame it's does not active. What I need to write at the init func that the windows will open?
Try pack(); method of JFrame. If you are planning to develop with Swing, I recommend you to follow this tutorial:
http://download.oracle.com/javase/tutorial/uiswing/index.html
You already have a JFrame (frame). so now you should add components for your panel (you may do it in the main class as well). Such components are JTextField, JButton, etc. (and even another JPanel) each component you can add to the panel using panel.add(component_name); it is also recommended to follow the tutorial as Erkan mentioned.
Your panel class should extend JPanel, not JFrame.
You can add components to JPanel such as JButton, JList, etc
This is an example code you need to init a JFrame if you don't create own classes:
public class LogMain
{
public static void main(String[] args)
{
JFrame window = new JFrame("Log");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setResizable(false);
JPanel panel = new JPanel();
JButton openFile = new JButton("Btn1");
JButton openDir = new JButton("Btn2");
panel.add(openFile);
panel.add(openDir);
window.add(panel);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
this is my example about the init of JFrame :
public class Windows{
public static void main(String args[]){
SJFrame window = new SJFrame("NEWNEWNEW");
window.init();
}
}
public class SJFrame extends JFrame(){
public SJFrame(String s){
super(s);
}
void init(){
Container panel = this.getContentPane();
panel.setBackground(Color.green);
panel.setLayout(new GridLayout(5,1));
JLabel jl1 = new JLabel("UserName");
JLabel jl2 = new JLabel("PassWord");
this.add(jl1);
this.add(jl2);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setSize(300,100);
this.pack();
this.setVisible(true);
}
}