我在Jpanel上制作10個星星,位置隨機。
但問題是,當我設定布局時,開始并沒有顯示出來。
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame() 。
frame.setSize(300, 300)。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
Container c = frame.getContentPane()。
c.setLayout(new BorderLayout()) 。
JPanel northPanel = new JPanel(new FlowLayout() )。
northPanel.add(new JButton("Open") )。
c.add(northPanel, BorderLayout.NORTH)。
JPanel southPanel = new JPanel(new FlowLayout() )。
southPanel.add(new JButton("Integer Input") )。
c.add(southPanel, BorderLayout.SOUTH)。
JPanel centerPanel = new JPanel(null)。
centerPanel.setBackground(Color.pink)。
for(int i = 0; i < 10; i ) {
int x = (int)(Math.random() * 300)。
int y = (int)(Math.random() * 300)。
JLabel label = new JLabel("*"/span>)。
label.setLocation(x, y);
label.setForeground(Color.green);
label.setOpaque(true)。
centerPanel.add(label)。
}
c.add(centerPanel, BorderLayout.CENTER)。
frame.setVisible(true)。
}
在粉色部分,應該顯示10顆星星
uj5u.com熱心網友回復:
我找到了我自己的方法。
之后設定標簽.setSize(10,10);
我可以在面板中找到10顆星星。
uj5u.com熱心網友回復:
基本上,每個容器如Jpanel、JFrame都使用了一些布局來放置組件,按照你的代碼,你將null作為引數傳遞給centralPanel,所以基本上中央面板沒有布局來放置組件。
注意:Flow Layout是一個default布局。
在這個案例中,讓嘗試以下步驟。
1.請使用setBound(x, y, height, width),在setLocation(x, y)的位置。
for (int i = 0; i < 10; i ) {
int x = (int)(Math.random() * 300)。
int y = (int)(Math.random() * 300)。
JLabel label = new JLabel("*") 。
**label.setBounds(x,y,10,10); **
label.setForeground(Color.green);
label.setOpaque(true)。
centerPanel.add(label)。
}
[1]: https://i.stack.imgur.com/UlUI.png
uj5u.com熱心網友回復:
如果你想在隨機位置放置10個字符(*),我建議通過自定義繪畫的方式來實作,如下圖所示:
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame() 。
//frame.setSize(300, 300); 不要設定大小,讓布局管理器來做。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
Container c = frame.getContentPane()。
/c.setLayout(new BorderLayout()); 不需要,它是默認的。
JPanel northPanel = new JPanel(new FlowLayout() )。/FlowLayout是默認的。
northPanel.add(new JButton("Open") )。
c.add(northPanel, BorderLayout.NORTH)。
JPanel southPanel = new JPanel(new FlowLayout() )。
southPanel.add(new JButton("Integer Input") )。
c.add(southPanel, BorderLayout.SOUTH)。
JPanel centerPanel = new PinkPlanet()。
c.add(centerPanel, BorderLayout.CENTER)。
frame.pack();
frame.setVisible(true)。
}
}
class PinkPlanet extends JPanel{
private static final int W = 300, H = 300;
private static final String STAR ="*"/span>。
private final Dimension pSize = new Dimension(W, H);
private final List<Point> locations = new ArrayList<>()。
public PinkPlanet() {
setBackground(Color.pink)。
for(int i = 0。i < 10; i ) {
int x =(int)(Math.random() * W)。
int y = (int)(Math.random() * H)。
locations.add(new Point(x, y)) 。
}
}
@Override; }
public Dimension getPreferredSize() {
return pSize。
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g)。
for(Point location : locations){
g.drawString(STAR, location.x, location.y)。
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/328060.html
標籤:

