有几次我因为建议使用以下方法而受到批评:
在Swing组件上.当我想在显示的组件之间定义比例时,我没有看到任何替代它们的用法.我被告知这个:
对于布局,答案总是相同的:使用合适的LayoutManager
我在网上搜索了一下,但我没有找到任何关于这个主题的综合分析.所以我有以下问题:
我想在Java GUI中显示一个树,但我不知道如何.树表示连接节点的图形,如下所示:
![]()
我应该说我有自己的树类:
public class BinaryTree
{
private BinaryNode root;
public BinaryTree( )
{
root = null;
}
public BinaryTree( Object rootItem )
{
root = new BinaryNode( rootItem, null, null );
}
public BinaryTree( Object rootItem,BinaryNode a,BinaryNode b )
{
root = new BinaryNode( rootItem, a, b );
}
public int leavesCount(){
return BinaryNode.leavesCount(root);
}
public boolean equal(BinaryTree a,BinaryTree b){
return BinaryNode.equal(a.root, b.root);
}
public void printPreOrder( )
{
if( root != null )
root.printPreOrder( );
}
public void …Run Code Online (Sandbox Code Playgroud) 
如何在屏幕上绘制半透明矩形?这不可能是JFrame因为JFrames在右上角有通常的关闭,最小化,最大化选项.
如果它确实是一个挥杆能力,它是如何在空气中绘制的?没有插入JFrame任何东西?请告诉我它是什么以及如何实现它......
我试图通过使用Action对象在我的GUI应用程序中将函数与state分开.我成功地使用它们来创建具有相同功能的菜单项和按钮.
我的问题是:我希望菜单中的"退出"项和框架的关闭按钮具有相同的操作.
目前我已经能够通过向框架添加以下WindowListener来解决它:
private class MainWindowListener extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
new ExitAction(model).actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Exit"));
}
}
Run Code Online (Sandbox Code Playgroud)
是不是有更简单直接的方法来做到这一点?
我希望通过覆盖它上面的鼠标事件来启用JLabel上的拖放功能,但是当我在mousePressed事件中定义拖放时,mouseReleased不会对该JLabel生效.难道我做错了什么 ?
Thumbnails[I_Loop].setText("1");
Thumbnails[I_Loop].setTransferHandler(new TransferHandler("text"));
Thumbnails[I_Loop].addMouseListener( new MouseAdapter() {
public void mouseReleased(MouseEvent me) {
System.out.println("here mouse released");
}
public void mousePressed(MouseEvent me) {
System.out.println("here mouse pressed");
JComponent comp = (JComponent) me.getSource();
TransferHandler handler = comp.getTransferHandler();
handler.exportAsDrag(comp, me, TransferHandler.COPY);
});
Run Code Online (Sandbox Code Playgroud)
*缩略图是JLabel的数组
运行程序时,拖放工作,但"此处鼠标已释放"语句不会打印.但是,当我从mousePressed()方法中删除负责DND的代码时,将打印"此处鼠标已释放".
这段代码有什么问题?
我正在学习Swing GUI设计.我还没有完全解决的一件事是如何添加添加Canvas到容器中的特定位置.
更具体地说:我创建了一个Canvas使用Paint方法的类.此类的对象将添加到Panel中.我不太明白的是,它是如何以及在何处添加到Panel中的.在Tkinter中Canvas是一个仅包含图像的小部件,但在Swing中,没有类似的小部件(可能不是最好的单词)添加到仅包含Canvas对象的Frame,而不包含任何其他内容.
对不起,如果它太模糊,我正在添加一个自包含的代码.请忽略文本字段和标签.
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
//frame class
class frame_class2 extends JFrame implements ActionListener{
//declare buttons
JButton draw_button = new JButton("Draw");
JButton quit_button= new JButton("Quit");
JButton info_button = new JButton("Info");
//declare labels
JLabel x_loc = new JLabel("X:");
JLabel y_loc = new JLabel("Y:");
JLabel w_label= …Run Code Online (Sandbox Code Playgroud) 我已经看到,在photoshop文本中只需拖动即可轻松调整大小.我们如何在Java中做同样的事情?关于如何在java中调整文本大小的任何想法?添加了在photoshop中调整大小的字母"A"的快照

请告诉我这段代码有什么问题?
public class ResizeImage extends JFrame {
public ResizeImage(){
JPanel panel = new JPanel(){
public void paintComponent(Graphics g) {
// In your paint(Graphics g) method
// Create a buffered image for use as text layer
BufferedImage textLayer = new BufferedImage(240, 240,
BufferedImage.TYPE_INT_RGB);
// Get the graphics instance of the buffered image
Graphics2D gBuffImg = textLayer.createGraphics();
// Draw the string
gBuffImg.drawString("Hello World", 10, 10);
// Rescale the string the way you want it
gBuffImg.scale(200, 50);
// Draw the buffered image …Run Code Online (Sandbox Code Playgroud) 这是我上一个问题的后续行动.我尽可能地简化了事情,但它仍然不起作用!虽然我使用的好东西getGraphics().
关于这里出了什么问题的详细解释非常受欢迎.我怀疑这是我addMouseListener()在这里使用方法的方式有问题.
编辑完全重写了代码.但仍然无法正常工作.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RunClass{
static MainClass1 inst1 = new MainClass1();
public static void main(String args[]){
JFrame frame1 = new JFrame();
frame1.add(inst1);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("NewPaintToolbox");
frame1.setSize(200, 200);
frame1.setLocationRelativeTo(null);
frame1.setVisible(true);
}
}
class MainClass1 extends JPanel implements MouseListener, MouseMotionListener{
int xvar=30;
int yvar=30;
//static PaintClass22 inst1 = new PaintClass22();
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
xvar …Run Code Online (Sandbox Code Playgroud) 我目前正在替换我的匿名ActionListeners
new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
使用表示操作的类文件:
public class xxxxxxxAction extends AbstractAction {
}
Run Code Online (Sandbox Code Playgroud)
但是,我的GUI能够执行很多操作(例如CreatePersonAction,RenamePersonAction,DeletePersonAction,SwapPeopleAction等).
有没有一种很好的方法将这些类组织成一个连贯的结构?