我正在尝试使用Canvas绘制简单的形状,在这个类中我设置了绘画
public class Game extends Canvas{
//FIELDS
public int WIDTH = 1024;
public int HEIGHT = WIDTH / 16 * 9;
//METHODS
public void start(){
Dimension size = new Dimension (WIDTH, HEIGHT);
setPreferredSize(size);
paint(null);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillOval(100, 100, 30, 30);
}
}
Run Code Online (Sandbox Code Playgroud)
在这个窗口
public class MainW {
public static void main(String[] args) {
Game ga = new Game();
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(ga);
frame.setVisible(true);
ga.start();
}
} …Run Code Online (Sandbox Code Playgroud) 这可能是一个奇怪的问题,所以我请求你们在这个问题上与我相提并论.
我想向一个人提供信息JFrame.特别是,a String.
这是捕获,它是一个非常长的String.比如,里程.总共想想几十行信息.
它有点像电话簿,其名称多于您可能适合的页面,并且您只能使用该页面.
所以我在想滚动条.
我试过了:
JTextArea area = new JTextArea(veryLongText.toString());
JScrollPane scrollPane = new JScrollPane(area);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为在我添加更多文本之前我无法向下滚动.这不是我想要的.
简单来说,我想要一个文本框,如果窗口太小而无法容纳其中的所有文本,我可以滚动它.
我该怎么做呢?
我将这些按钮放在框架的中心并且彼此上方,就像这样.
BUTTON
BUTTON
BUTTON
Run Code Online (Sandbox Code Playgroud)
我在这个论坛上搜索了多个主题,但到目前为止我尝试过的所有内容都没有用.我希望有人有解决方案.
这是我目前的代码:
package ípsen1;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class Paneel extends JPanel implements ActionListener {
Image achtergrond;
private JButton spelHervatten;
private JButton spelOpslaan;
private JButton spelAfsluiten;
public Paneel(){
//buttons
spelHervatten = new JButton("Spel hervatten");
spelHervatten.setPreferredSize(new Dimension(380, 65));
spelOpslaan = new JButton("Spel opslaan");
spelOpslaan.setPreferredSize(new Dimension(380, 65));
spelAfsluiten = new JButton("Spel afsluiten");
spelAfsluiten.setPreferredSize(new Dimension(380, 65));
//object Paneel luistert naar button events
spelAfsluiten.addActionListener(this);
add (spelHervatten);
add …Run Code Online (Sandbox Code Playgroud) 我有这个我无法处理的小虫子.我将JPanel的大小设置为(150,90),但paintComponent()中的代码显示为(160,100).怎么了?谢谢你的时间和帮助.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test2 extends JPanel{
public static void main(String[] args) {
JFrame window = new JFrame("Test");
Test2 content = new Test2();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.pack();
window.setResizable(false);
window.setVisible(true);
}
Test2(){
setBackground( new Color(0,120,0) );
setPreferredSize( new Dimension(150, 90));
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor( Color.YELLOW);
g.drawRect(0, 0, 150, 90);
System.out.println(this.getWidth());
System.out.println(this.getHeight());
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出表明大小为(160,90).黄色矩形显示相同.
根据我的要求,UI应该显示如下图所示。
我试图通过使用下面的代码来达到相同的目的。
package com.samples;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
public class MyFrame extends JFrame {
public MyFrame() {
}
public static void main(String[] args) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("My Sample Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 800);
frame.setVisible(true);
JPanel jpOPanel = new JPanel();
jpOPanel.setBorder(new CompoundBorder(new …Run Code Online (Sandbox Code Playgroud) 我有一个JPanel并设置如下:
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
Run Code Online (Sandbox Code Playgroud)
然后我添加一个JTextField占用整个面板的:
JTextField field = new JTextField();
panel.add(field);
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试调整它的大小时:
panel.setPreferredSize(new Dimension(20,400));
Run Code Online (Sandbox Code Playgroud)
什么都没发生.为什么?我正在使用BoxLayout以便将我JLabel和JTextField组件按垂直顺序排列.如果无法调整面板的大小,我是否可以至少调整大小JTextField以使其不占用整个空间?
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUI extends JFrame
{
public GUI() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.setPreferredSize(new Dimension(20,400));
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
panel.add(field1);
panel.add(field2);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在为我的程序中的错误消息设置一个JLabel,所以最初标签是空的label.setText(""),但是当出现错误时它应该改为类似的label.setText("Error, you have entered invalid data...").
如果我setSize(x,y)在标签上使用它,则会在发生错误消息时强制其他组件移位.但使用setPreferredSize(Dimension(x,y))并不影响他们.
Q1.这是为什么?
Q2.setSize(x,y)和之间有什么区别setPreferredSize(Dimension(x,y))
Q3.是否必须对布局做任何事情?
提前谢谢您的解释!
PS我正在使用GridBagLayout来定位我的组件JPanel.
public ButtonGrid(int width, int length){ //constructor
frame.setTitle("MPC");
frame.setLayout(new GridLayout(width,length)); //set layout
grid=new JButton[width-1][length]; //allocate the size of grid
for(int y=0; y<length; y++){
for(int x=0; x<width-1; x++){
grid[x][y]=new JButton("("+x+","+y+")"); //creates new button
frame.add(grid[x][y]); //adds button to grid
grid[x][y].addActionListener(this);
grid[x][y].addKeyListener(this);
//grid[x][y].setMnemonic(KeyEvent.VK_0);
grid[x][y].setPreferredSize(new Dimension(75,75));
}
}
for(int i =0; i<boxList.length; i++)
box.addItem(boxList[i]);
box.addActionListener(this);
frame.add(box);
frame.add(met_speed);
frame.add(Play);
Play.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
public void newWindow(){
JFrame frame1 = new JFrame();
frame1.setLayout(new GridBagLayout());
GridBagConstraints gbc = new …Run Code Online (Sandbox Code Playgroud) 我有一个通常有效的设置,但我现在发现它有点痛苦.
我有一个JDialog(以前是一个JFrame,但我最近更改了UI流程以删除冗余JFrames)设置为BorderLayout,其中包含三个主要JPanels,标题,内容和页脚.
标题JPanel有一个正常加载的背景图像.但是,我正在调用setMinimumSize/ setPreferredSize/ etc的各种方式(我甚至试过setSize并且setBounds绝望了)并且JPanel没有正确调整大小.该组件的布局是BoxLayout,并且孩子们可以舒适地坐在其中,但这不应该影响我正在尝试使用标题面板本身.
唯一有效的方法是在父级上设置大小JDialog.但我不想这样做/从我所看到的看起来像是糟糕的设计.我还需要计算所有孩子的潜在宽度/高度,添加边距等.
建议我不是在寻找:"使用不同的LayoutManager."
我想了解是否有原因导致子组件不受尊重.
我可以提供代码,但是考虑到我正在处理的互锁系统的数量,隔离一个小的,可运行的段很困难.这是设置大小的相关代码段JPanel.图像尺寸为480*96.
public JPanelThemed(BufferedImage image) {
super();
this.backgroundImage = image;
setAllSimilarConstraints(new Dimension(image.getWidth(), image.getHeight()));
setOpaque(false);
}
// for use with BoxLayout as it requires explicit bounds to be set
public void setAllSimilarConstraints(Dimension dim) {
setPreferredSize(dim);
setMinimumSize(dim);
setMaximumSize(dim);
}
Run Code Online (Sandbox Code Playgroud) 我的Java有问题 据我所知,Java中绘制几何图形不是来的,代码和下面的你能帮我吗?
这是代码:
public class Gioco {
public static void main (String args [])
{
PaintRect();
}
public static void PaintRect() {
g.drawRect(100,100,300,300);
g.drawLine(100,100,100,100);
g.setBackground(Color.BLACK);
System.out.println("Trasut");
credits.setText("Kitebbiv");
credits.setBackground(null);
credits.setEditable(false);
credits.setFocusable(false);
credits.setBounds(0,0,100,100);
credits.setForeground(Color.BLACK);
panel.add(credits);
g.getPaint();
}
Run Code Online (Sandbox Code Playgroud)
如何创建JFrame三角形、正方形和矩形?更正我的代码谢谢
我正在编写一些代码。JFrame包含JPanel具有特定尺寸的。这是我的代码:
import javax.swing.*;
import java.awt.*;
public class ScrollPane extends JFrame {
public ScrollPane() {
super("Title");
setLayout(new BorderLayout());
setSize(320,240);
JScrollPane scroller = new JScrollPane(new DrawingPane());
scroller.setPreferredSize(new Dimension(320,240));
add(scroller);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private class DrawingPane extends JPanel {
public DrawingPane(){
super();
setSize(640,480);
setMinimumSize(new Dimension(320,240));
}
}
public static void main(String[] args) {
new ScrollPane();
}
}
Run Code Online (Sandbox Code Playgroud)
即使设置了最小尺寸JPanel,卷轴也不会出现。