need to init the JFrame - java

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);
}
}

Related

How can I make this example with java GUI JFrame?

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>");

Custom JPanel class does not show up in BoxLayout of Container

Selected code from SCMain.java:
public JPanel createContentPane() {
//Create the content-pane-to-be.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
return contentPane;
}
public JPanel populateContentPane() {
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JPanel panel1 = new AddAccountForm(this, this.getInputSet());;
JPanel panel2 = new JPanel();
container.add(Box.createRigidArea(new Dimension(0, 5)));
container.add(panel1);
container.add(Box.createRigidArea(new Dimension(0, 5)));
container.add(panel2);
container.add(Box.createGlue());
return container;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Sole Commando v1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
frame.setJMenuBar(this.createMenuBar());
frame.setContentPane(this.createContentPane());
// Add split panels
frame.add(populateContentPane(), BorderLayout.CENTER);
frame.pack();
//Display the window.
frame.setSize(1080, 1080);
frame.setVisible(true);
}
Selected code from AddAccountForm.java:
public class AddAccountForm extends JPanel implements ActionListener{
AddAccountForm(SCMain main, Set<String> InputSet) {
//Combobox setup
setSize(300, 300);
System.out.println(Arrays.toString(storeNameList.toArray()));
submitButton.addActionListener(this);
}
public JPanel getAddAccountRoot() {
return addAccountRoot;
}
private void createUIComponents() {
// TODO: place custom component creation code here
storeNames = new JComboBox();
}
}
I tested using the AddAccountForm.java as a JFrame (extend JFrame instead of extend JPanel, and adding pack(), setContentPane(addAccountRoot) to AddAccountForm.java) and it brought up the correct AddAccountForm GUI if I just did:
SCMain new1 = new SCMain();
AddAccountForm new2 = new AddAccountForm(new1, new1.getInputSet());
However, when using it as a JPanel (panel1 in the above SCMain.java code) and running SCMain, AddAccountForm GUI does not show up at all.
Note: The JPanel AddAccountForm was created in IntelliJ GUI Builder, but as I said previously it works as a JFrame so the code must be somewhat correct.

How To Have A TabPane For A ScrollPane With A TextPane in Java

I'm creating a notepad program in Java and would like to have different tabs to for each document opened. I'm having trouble getting the tabs to be displayed. This is my test document so far that I have looked over here at first and I modified it for the text document. How to add a JScrollPane onto a JTabbedPane using a null layout?.
This is what I currently have:
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
private static void CreateAndShowGui() {
JFrame frame = new JFrame("Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane txt = new JTextPane();
JPanel noWrapPanel = new JPanel(new BorderLayout());
noWrapPanel.add(txt);
JScrollPane scroll = new JScrollPane(noWrapPanel);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
frame.add(topPanel);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Welcome", panel);
topPanel.add(tabbedPane);
frame.add(scroll);
frame.setSize(400, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UIManager.put("swing.boldmetal", Boolean.FALSE);
CreateAndShowGui();
}
});
}
}
What am I doing wrong? :(
you are adding scroll pane to frame .
frame.add(scroll);
but you should add scroll pane to jpanel and add pane to scroll pane.you have done that part.
so remove this incorrect line.
frame.add(scroll);
this is complete code without using extends jframe .
this is complete code using extends jframe
note: you have extends your class by jframe class but you are creating a new frame .you don't need to create a frame variable .you can either remove extends part or directly use your class as a jframe without creating a new frame.

JScrollPane in JFrame

I want to write a simple Java program, which consists of a JFrame that integrates a JScrollPane. Just it does not work the way I do it.
What is the issue of the my approach ?
public class TestView {
JFrame frame;
JScrollPane scrollPane;
public TestView(){
frame = new JFrame();
scrollPane = new JScrollPane();
scrollPane.add(new JLabel("Klick me"));
scrollPane.setMinimumSize(new Dimension(200,200));
frame = new JFrame();
frame.getContentPane().add(scrollPane);
frame.setSize(200,200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void createAndShowGui(){
TestView tv = new TestView();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGui();
}
});
If the issue is that you do not see your label in the scrollpane, you might need to use
scrollpane.setViewportView(new JLabel("Klick me"));
instead of
scrollPane.add(new JLabel("Klick me"));
Additionally, I suggest you create a JPanel, give it a layout, and place your label there, instead of passing the label to the scrollpane. Then set this panel as the viewport.
Please see
Difference between JscrollPane.setviewportview vs JscrollPane.add
use for example:
final JPanel myPanel = new JPanel();
myPanel.setPreferredSize(new Dimension(50, 50));
final JScrollPane scrollPane = new JScrollPane(myPanel);
setMinimumSize will be ignored.

Why not jframe shows button?

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.

Categories

Resources