我是二年级学生,我正在研究我的OOP项目(计算器).我已经完成了数字按钮和运算符的功能.现在我正处于重新安排按钮的阶段.首先,我只是将按钮大小设置为(50,50),它工作正常,其标签仍然可见,但当我决定将其缩小(30,30)时,其标签变为"..."而不是.
这是照片:

继承我的代码:
lblEdit.setBounds(-138,-5,180,50);
lblView.setBounds(-90,-5,180,50);
lblHelp.setBounds(-40,-5,180,50);
txt.setBounds(15,35,250,30); // text pane
txt2.setBounds(0,330,100,20);
blank.setBounds(15,80,30,30); // this is just an extra button, no use at all, OK? :D
btnMC.setBounds(15,115,30,30);
btnMR.setBounds(15,150,30,30);
btnMS.setBounds(15,185,30,30);
btnMp.setBounds(15,220,30,30);
Run Code Online (Sandbox Code Playgroud) java user-interface swing layout-manager null-layout-manager
我试图做两个按钮JFrame,我调用setBounds它们的方法来设置它们的位置和大小,我也传递null给setLayout1 because I want to use组件的setBounds`方法.
现在我想用我的代码做一些事情,每当我调整框架按钮时,装饰将以适当的形式改变,如下面的图片:

我知道可以使用从JPanel类中创建一个对象并向其添加按钮,最后将创建的面板对象添加到框架中,但由于某种原因(由教授指定),我现在不允许这样做.
有什么办法或者你有什么建议吗?
我的代码是这样的:
public class Responsive
{
public static void main(String[] args)
{
JFrame jFrame = new JFrame("Responsive JFrame");
jFrame.setLayout(null);
jFrame.setBounds(0,0,400,300);
JButton jButton1 = new JButton("button 1");
JButton jButton2 = new JButton("button 2");
jButton1.setBounds(50,50,100,100);
jButton2.setBounds(150,50,100,100);
jFrame.add(jButton1);
jFrame.add(jButton2);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
public class MainClass extends Component{
private Image bg;
private ImageIcon newgame;
private ImageIcon quit;
private ImageIcon options;
private JButton bquit;
private JButton boptions;
private JButton bnewgame;
private static Container pane; //Container
public void loadImage() {
try {
bg=ImageIO.read(new File("bg1.png"));
} catch (Exception e) {
}
if(bg!=null)
repaint();
}
public void paint(Graphics g) {
g.drawImage(bg,0,0,null);
}
public void imageButtons(JFrame f) {
try {
quit= new ImageIcon("quit.png");
options=new …Run Code Online (Sandbox Code Playgroud) 所以我有一个叫做CalendarPanel扩展的类JPanel.它使用null布局.我如何CalendarPanel用作常规组件?当我把它放在另一个JPanel然后将其添加到窗口时,它会消失.它只有在我直接添加到窗口时才可见.
编辑:是的,我意识到使用JPanelnull布局是不好的做法.CalendarPanel实际上是别人的代码,我试图将它用于我的目的,而不必重构它.
我使用以下代码将JTextArea添加到我的JPanel:
commentTextArea.setLineWrap(true);
commentTextArea.setWrapStyleWord(true);
commentTextArea.setVisible(true);
this.add(commentTextArea);
commentTextArea.setBounds(0, 0, 100, 100);
//commentTextArea.setLocation(0, 0);
Run Code Online (Sandbox Code Playgroud)
每当我使用setLocation(0,0)时,JTextArea都不会移动.它始终位于屏幕的顶部中间,而不是(0,0).setBounds(0,0,100,100)也是如此,但高度和宽度都是这样设置的,而不是位置.为什么是这样?
完整代码
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class UMLEditor {
public static void main(String[] args) {
JFrame frame = new UMLWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(30, 30, 1000, 700);
frame.getContentPane().setBackground(Color.white);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class UMLWindow extends JFrame {
Canvas canvas = new Canvas();
private static final long serialVersionUID = 1L;
public UMLWindow() {
addMenus();
}
public …Run Code Online (Sandbox Code Playgroud) 我不明白问题出在哪里.JTable嵌入在嵌入JPanel的JScrollPanel中.该表未显示.任何帮助赞赏.我可能错过了添加一些元素.彻底检查但找不到任何东西.这只是构造函数:
public TableIssues() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 894, 597);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel patientsPanel = new JPanel();
patientsPanel.setBounds(6, 152, 882, 417);
patientsPanel.setLayout(null);
String[] patientsColumns = {
"one",
"two",
"three"};
String[][] tableInput={{"first","second","third"},{"first","second","third"}};
patientsTable = new JTable(tableInput,patientsColumns);
JScrollPane scroll = new JScrollPane();
scroll.setBounds(0, 0, 882, 363);
scroll.setLayout(null);
scroll.setViewportView(patientsTable);
patientsPanel.add(scroll);
JButton addPatietsButton = new JButton("Add");
addPatietsButton.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
addPatietsButton.setBounds(356, 375, 211, 36);
patientsPanel.add(addPatietsButton);
contentPane.add(patientsPanel);
}
Run Code Online (Sandbox Code Playgroud) 我有一个 JLayeredPane。我的程序是这样工作的:
JPanel p1 = new JPanel(new BoxLayout(p1, BoxLayout.X_AXIS));
JPanel p2 = new JPanel(new BoxLayout(p2, BoxLayout.Y_AXIS));
JLayeredPane lp = new JLayeredPane();
lp.add(p1, 1);
lp.add(p2, 0);
Run Code Online (Sandbox Code Playgroud)
p1 和 p2 都有按钮等组件...
问题是,当我将两个 JPanel 添加到 JLayeredPane 时,什么也没有出现。
我尝试更改 JLayeredPane() 的布局。
例如,我做了:
lp.setLayout(new BoxLayout(lp, BoxLayout.X_AXIS));
Run Code Online (Sandbox Code Playgroud)
然后,JPanels 确实显示,但它们显示为相邻,不尊重 JLayeredPane 的层。
我是否被迫使用空布局?
如何让我的 JLayeredPane 尊重图层并正确显示我的两个 BoxLayout JPanel?
当我给我的 JLayeredPane 一个布局时,它会显示面板,但它根本不尊重图层。
嗨,我正在尝试制作 Java 桌面应用程序,其中我使用多个 jlabel 我想在每个标签之间留出很小的空间
我怎样才能做到这一点
这是我的代码
public class Second extends javax.swing.JFrame {
JLabel label=new JLabel();
/**
* Creates new form Second
*/
public Second() {
this.getContentPane().setBackground(new java.awt.Color(255, 140, 0));
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setUndecorated(true);
JButton print= new JButton();
print.setBackground(new java.awt.Color(153, 153, 0));
print.setOpaque(true);
print.setBounds(525,1282,130,85);
print.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
print.setForeground(new java.awt.Color(255,255,255));
print.setText("Print");
this.add(print);
JButton home= new JButton();
home.setBackground(new java.awt.Color(153, 153, 0));
home.setOpaque(true);
home.setBounds(640,1282,130,85);
home.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
home.setForeground(new java.awt.Color(255, 255,255 ));
home.setText("Home");
this.add(home);
int Height …Run Code Online (Sandbox Code Playgroud) 我在Java Swing中遇到这个问题:

洋红色和绿色成分都是JButtons.我正在使用绝对布局.当悬停到绿色时,即使没有应用布局管理器也不使用JLayeredPane,它与Magenta重叠.
这种行为的原因是什么?如何在悬停到绿色时确保洋红色保持在顶部?
编辑2: 为了明确我的目标,我们的想法是创建一个类似于带有辅助触摸的Android通知栏的用户界面.假设通知栏是一个图层,而辅助触摸是最顶层.在JLayeredPane中使用透明层的问题是,如果图层/面板即使设置为透明也占据整个框架,则不会绘制其下面的图层.
我是 Java Swing 的业余爱好者,无法解决以下问题。
一旦我添加JScrollPane到JTextArea,GUI 中就没有任何组件可见。
我知道在添加滚动条时不应该添加文本区域(我注释掉了该行),但这无济于事。
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(213, 11, 186, 240);
// NOT CALLING frame.getContentPane().add(textArea);
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);
Run Code Online (Sandbox Code Playgroud)
它仅在我使用时对我有用BorderLayout,但这不是我想使用的布局。
我的目标是在框架中放置几个文本区域。
我该怎么做才能通过滚动显示文本区域,例如AbsoluteLayout(null)?
java ×10
swing ×10
css-position ×1
jbutton ×1
jframe ×1
jlabel ×1
jlayeredpane ×1
jtable ×1
mouseevent ×1
setbounds ×1