这是我的第一篇帖子,请原谅我,如果我做错了.
我的问题是:
我正在尝试将组件添加到具有Size等定义值的JPanel中.但是当我将它们添加到Panel时,它们绝对没有它们应该具有的大小和位置.例如:
public class Console extends JFrame {
private JPanel mainPanel = new JPanel();
private JTextArea textField = new JTextArea();
private JTextArea textField2 = new JTextArea();
public Console() {
this.setSize(500,300);
this.mainPanel.setSize(this.getWidth(),this.getHeight());
this.textField.setEditable(false);
this.textField.setSize(this.mainPanel.getWidth(), 100);
this.textField.setPreferredSize(new Dimension(this.mainPanel.getWidth(),this.mainPanel.getHeight()));
this.textField.setLocation(0, 0);
this.textField.setText("some text");
this.textField.setVisible(true);
this.textField2.setSize(this.mainPanel.getWidth(),200);
this.textField2.setPreferredSize(new Dimension(this.getWidth(),this.getHeight()));
this.textField2.setLocation(0,this.mainPanel.getHeight()-this.textField.getHeight());
this.textField2.setText("blabla");
this.textField2.setVisible(true);
this.mainPanel.add(textField);
this.mainPanel.add(textField2);
this.mainPanel.setVisible(true);
this.add(this.mainPanel);
// I know you should not call setVisible() in the Constructor, just for making Code more simple here.
this.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序时,两个JTextArea都非常小,位于中间(不是上面设置),而mainPanel是正确的.我试图在代码中的不同位置调用setSize()和setPreferredSize(),但它没有'工作.据我所知,最好使用LayoutManager来做到这一点,但说实话,我不知道如何正确使用它.我在Oracle Doc上检查了它,但如果有人可以为此发布一个干净的解决方案我会很感激,谢谢你提前.
我最近决定开始学习 Python,在做几个小项目作为实践方法时,我发现了这个customtkinter库 ( https://github.com/TomSchimansky/CustomTkinter ),可以使用 Python 进行更现代的 GUI 开发。
我想做的事情要么需要文件的拖放组件,要么需要文件选择器对话框,这似乎在带有模块的原始库中有所存在,但似乎在文档中没有直接提及库包装器。tkintertkinterdnd2customtkinter
有谁知道如何专门使用拖放文件customtkinter?
如果没有 的直接包装器customtkinter,有没有办法将 的样式应用customtkinter到tkinderdnd2模块?当像这样使用它时,显然它只是使用默认tkinter样式:
from tkinter import TOP, Entry, Label, StringVar
from tkinterdnd2 import *
def get_path(event):
pathLabel.configure(text = event.data)
root = TkinterDnD.Tk()
root.geometry("350x100")
root.title("Get file path")
nameVar = StringVar()
entryWidget = Entry(root)
entryWidget.pack(side=TOP, padx=5, pady=5)
pathLabel = Label(root, text="Drag and drop file in the entry box")
pathLabel.pack(side=TOP)
entryWidget.drop_target_register(DND_ALL)
entryWidget.dnd_bind("<<Drop>>", get_path)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)