[英]I can't move the buttons to the bottom - JFrame
I tried using GridLayout and BorderLayout but could not make my Buttons go below the mouth of the dog (picture). 我尝试使用GridLayout和BorderLayout,但是无法使Buttons脱离狗的嘴(图片)。 After making the button below the dog mouth, how do I add function to the buttons?
将按钮置于狗嘴下方之后,如何为按钮添加功能? For Settings, how do I make it open another window after I clicked on it and I can close the settings window instead of the whole thing.
对于“设置”,如何在单击它后打开另一个窗口,并且可以关闭设置窗口而不是整个窗口。 Thank you!
谢谢!
Here is my code: 这是我的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Codegi extends JFrame {
public Codegi() {
JFrame frame = new JFrame();
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("enter image description here[1]")))));
} catch (IOException e) {
e.printStackTrace();
}
frame.pack();
frame.setLayout(new FlowLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.setOpaque(false);
p2.setOpaque(false);
p1.setLayout(new BorderLayout(0, 20));
//JButton b1 = new JButton("START");
//JButton b2 = new JButton("SETTINGS");
p2.add(p1);
p1.add(new JButton("SETTINGS"), BorderLayout.SOUTH);
p1.add(new JButton("START"), BorderLayout.CENTER);
//p1.add(b1);
//p1.add(b2);
frame.add(p2);
//design of button
//Font largefont = new Font("TimesRoman", Font.BOLD, 20);
//b1.setBackground(Color.ORANGE);
//b2.setBackground(Color.ORANGE);
//b1.setForeground(Color.BLACK);
//b2.setForeground(Color.BLACK);
//b1.setFont(largefont);
//b2.setFont(largefont);
frame.setTitle("Codegi:Programming made fun");
frame.setSize(498, 687);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(467, 627);
}
public static void main(String args[]) {
new Codegi();
}
}
Set the layout to null view.setLayout(null); 将布局设置为null view.setLayout(null); and then set the position of buttons like below btn.setBounds(starting x-coordinate,starting y-coordinate,btn width,btn height);
然后像下面的btn.setBounds一样设置按钮的位置(开始x坐标,开始y坐标,btn宽度,btn高度); remove panels by setVisible(false);
通过setVisible(false)删除面板; and show new ones with setVisible(true);
并使用setVisible(true)显示新的; on btn click event
在btn点击事件上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.