如何添加两个组件,使它们彼此独立?
How can I add two components so they are both independent of each other?
所以我正在创建一个屏幕,其中的球会在屏幕上弹跳。我想在不破坏弹跳球物理特性的情况下将文本放置在屏幕中央。我的解决方案是使用两个独立的布局管理器(一个用于球,一个用于文本),但它会弄乱整个面板。这是我的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game04 extends JPanel {
static Random random = new Random();
static int max = 370;
static int min = 0;
static int x = min + random.nextInt(max - min + 1);
static int y = min + random.nextInt(max - min + 1);
static int num = min + random.nextInt(max - min + 1);
static int xValues[] = {num,num,num,num,num,num,num,num,num};
static int yValues[] = {num,num,num,num,num,num,num,num,num};
static int xa[] = {1,-1,1,-1,1,-1,1,-1,1};
static int ya[] = {1,-1,1,-1,1,-1,1,-1,1};
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
Game04 game = new Game04();
frame.setLayout(new BorderLayout());
frame.add(game);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
for(int i = 0; i < 9; i++) {
num = min + random.nextInt(max - min + 1);
xValues[i] = num;
num = min + random.nextInt(max - min + 1);
yValues[i] = num;
}
while(true) {
game.moveBall();
game.repaint();
Thread.sleep(30);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Sets anti-antialiasing
RenderingHints.VALUE_ANTIALIAS_ON); // for drawing the tool
for(int i = 0; i < 9; i++) {
g2d.setColor(Color.GREEN);
g2d.fillOval(xValues[i],yValues[i],30,30);
}
}
private void moveBall() {
for(int i = 0; i < 9; i++) {
if(xValues[i] + xa[i] < 0)
xa[i] = 1;
if(xValues[i] + xa[i] > getWidth() - 30) // 30px is size of the oval object
xa[i] = -1;
if(yValues[i] + ya[i] < 0)
ya[i] = 1;
if(yValues[i] + ya[i] > getHeight() - 30) // 30px is size of the oval object
ya[i] = -1;
xValues[i] = xValues[i] + xa[i];
yValues[i] = yValues[i] + ya[i];
}
}
}
这是我尝试添加以在屏幕上创建文本的代码:
JTextArea quoteText = new JTextArea();
quoteText.setText("Default Text");
JPanel borderPane = new JPanel();
borderPane.add(quoteText, BorderLayout.CENTER);
JPanel masterPane = new JPanel();
masterPane.add(game, null);
masterPane.add(borderPane, BorderLayout.CENTER);
frame.add(masterPane);
唉,它把一切都搞砸了。我知道有更好的解决方案。
我建议以与渲染球相同的方式渲染文本 - 使用 Graphics
对象,在覆盖的 paint()
方法中。它有一个 drawString
方法,可以在不添加全新组件和破坏布局的情况下达到预期效果。
使用构造函数添加JTextArea
public Game04() {
//layout manager will is used for JTextArea only
setLayout(new GridBagLayout());
JTextArea quoteText = new JTextArea();
quoteText.setText("Default Text");
add(quoteText);
}
+1 对于格式正确的问题。
与您的问题无关:我会将代码结构更改为:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class Game04 extends JFrame {
static Random random = new Random();
static int max = 370;
static int min = 0;
static int x = min + random.nextInt((max - min) + 1);
static int y = min + random.nextInt((max - min) + 1);
static int num = min + random.nextInt((max - min) + 1);
static int xValues[] = {num,num,num,num,num,num,num,num,num};
static int yValues[] = {num,num,num,num,num,num,num,num,num};
static int xa[] = {1,-1,1,-1,1,-1,1,-1,1};
static int ya[] = {1,-1,1,-1,1,-1,1,-1,1};
Game04(){
super("Mini Tennis");
Game04Panel game = new Game04Panel();
setLayout(new BorderLayout());
add(game, BorderLayout.CENTER);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
for(int i = 0; i < 9; i++) {
num = min + random.nextInt((max - min) + 1);
xValues[i] = num;
num = min + random.nextInt((max - min) + 1);
yValues[i] = num;
}
while(true) {
game.moveBall();
game.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Game04();
}
class Game04Panel extends JPanel {
public Game04Panel() {
//layout manager will is used for JTextArea only
setLayout(new GridBagLayout());
JTextArea quoteText = new JTextArea();
quoteText.setText("Default Text");
add(quoteText);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Sets anti-antialiasing
RenderingHints.VALUE_ANTIALIAS_ON); // for drawing the tool
for(int i = 0; i < 9; i++) {
g2d.setColor(Color.GREEN);
g2d.fillOval(xValues[i],yValues[i],30,30);
}
}
private void moveBall() {
for(int i = 0; i < 9; i++) {
if((xValues[i] + xa[i]) < 0) {
xa[i] = 1;
}
if((xValues[i] + xa[i]) > (getWidth() - 30)) {
xa[i] = -1;
}
if((yValues[i] + ya[i]) < 0) {
ya[i] = 1;
}
if((yValues[i] + ya[i]) > (getHeight() - 30)) {
ya[i] = -1;
}
xValues[i] = xValues[i] + xa[i];
yValues[i] = yValues[i] + ya[i];
}
}
}
}
所以我正在创建一个屏幕,其中的球会在屏幕上弹跳。我想在不破坏弹跳球物理特性的情况下将文本放置在屏幕中央。我的解决方案是使用两个独立的布局管理器(一个用于球,一个用于文本),但它会弄乱整个面板。这是我的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game04 extends JPanel {
static Random random = new Random();
static int max = 370;
static int min = 0;
static int x = min + random.nextInt(max - min + 1);
static int y = min + random.nextInt(max - min + 1);
static int num = min + random.nextInt(max - min + 1);
static int xValues[] = {num,num,num,num,num,num,num,num,num};
static int yValues[] = {num,num,num,num,num,num,num,num,num};
static int xa[] = {1,-1,1,-1,1,-1,1,-1,1};
static int ya[] = {1,-1,1,-1,1,-1,1,-1,1};
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Mini Tennis");
Game04 game = new Game04();
frame.setLayout(new BorderLayout());
frame.add(game);
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
for(int i = 0; i < 9; i++) {
num = min + random.nextInt(max - min + 1);
xValues[i] = num;
num = min + random.nextInt(max - min + 1);
yValues[i] = num;
}
while(true) {
game.moveBall();
game.repaint();
Thread.sleep(30);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Sets anti-antialiasing
RenderingHints.VALUE_ANTIALIAS_ON); // for drawing the tool
for(int i = 0; i < 9; i++) {
g2d.setColor(Color.GREEN);
g2d.fillOval(xValues[i],yValues[i],30,30);
}
}
private void moveBall() {
for(int i = 0; i < 9; i++) {
if(xValues[i] + xa[i] < 0)
xa[i] = 1;
if(xValues[i] + xa[i] > getWidth() - 30) // 30px is size of the oval object
xa[i] = -1;
if(yValues[i] + ya[i] < 0)
ya[i] = 1;
if(yValues[i] + ya[i] > getHeight() - 30) // 30px is size of the oval object
ya[i] = -1;
xValues[i] = xValues[i] + xa[i];
yValues[i] = yValues[i] + ya[i];
}
}
}
这是我尝试添加以在屏幕上创建文本的代码:
JTextArea quoteText = new JTextArea();
quoteText.setText("Default Text");
JPanel borderPane = new JPanel();
borderPane.add(quoteText, BorderLayout.CENTER);
JPanel masterPane = new JPanel();
masterPane.add(game, null);
masterPane.add(borderPane, BorderLayout.CENTER);
frame.add(masterPane);
唉,它把一切都搞砸了。我知道有更好的解决方案。
我建议以与渲染球相同的方式渲染文本 - 使用 Graphics
对象,在覆盖的 paint()
方法中。它有一个 drawString
方法,可以在不添加全新组件和破坏布局的情况下达到预期效果。
使用构造函数添加JTextArea
public Game04() {
//layout manager will is used for JTextArea only
setLayout(new GridBagLayout());
JTextArea quoteText = new JTextArea();
quoteText.setText("Default Text");
add(quoteText);
}
+1 对于格式正确的问题。
与您的问题无关:我会将代码结构更改为:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
@SuppressWarnings("serial")
public class Game04 extends JFrame {
static Random random = new Random();
static int max = 370;
static int min = 0;
static int x = min + random.nextInt((max - min) + 1);
static int y = min + random.nextInt((max - min) + 1);
static int num = min + random.nextInt((max - min) + 1);
static int xValues[] = {num,num,num,num,num,num,num,num,num};
static int yValues[] = {num,num,num,num,num,num,num,num,num};
static int xa[] = {1,-1,1,-1,1,-1,1,-1,1};
static int ya[] = {1,-1,1,-1,1,-1,1,-1,1};
Game04(){
super("Mini Tennis");
Game04Panel game = new Game04Panel();
setLayout(new BorderLayout());
add(game, BorderLayout.CENTER);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
for(int i = 0; i < 9; i++) {
num = min + random.nextInt((max - min) + 1);
xValues[i] = num;
num = min + random.nextInt((max - min) + 1);
yValues[i] = num;
}
while(true) {
game.moveBall();
game.repaint();
try {
Thread.sleep(30);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Game04();
}
class Game04Panel extends JPanel {
public Game04Panel() {
//layout manager will is used for JTextArea only
setLayout(new GridBagLayout());
JTextArea quoteText = new JTextArea();
quoteText.setText("Default Text");
add(quoteText);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Sets anti-antialiasing
RenderingHints.VALUE_ANTIALIAS_ON); // for drawing the tool
for(int i = 0; i < 9; i++) {
g2d.setColor(Color.GREEN);
g2d.fillOval(xValues[i],yValues[i],30,30);
}
}
private void moveBall() {
for(int i = 0; i < 9; i++) {
if((xValues[i] + xa[i]) < 0) {
xa[i] = 1;
}
if((xValues[i] + xa[i]) > (getWidth() - 30)) {
xa[i] = -1;
}
if((yValues[i] + ya[i]) < 0) {
ya[i] = 1;
}
if((yValues[i] + ya[i]) > (getHeight() - 30)) {
ya[i] = -1;
}
xValues[i] = xValues[i] + xa[i];
yValues[i] = yValues[i] + ya[i];
}
}
}
}