- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不明白为什么 JScrollpane 不会被添加到 JTextArea,这是因为某种布局问题吗?
这是我 friend 做的一个文本编辑器,他一开始只用AWT做的,后来我用swing的JTextArea替换了AWT TextArea来换行。
输出:
编辑:感谢所有为我投入时间的人。我发现将 JTextArea 添加到 Frame 才是真正的问题,因为它已经添加到了 JScrollPane 中;并且 JScrollPane 已添加到 Frame 中。所以我只是删除了将 JTextArea 添加到框架的行,该行写在我在代码中创建主题的上方。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
//---------------------------------------
class MyFrame extends JFrame { // creating class name is 'Myframe' extending from 'JFrame' class
MenuBar bar;
Menu menu1, menu2, format_menu, font_size, theme;
MenuItem new_item1, item2, item3, item4, item5, item6, item7, item8;
MenuItem dracula, queen, dawn, light;
MenuItem size_8, size_12, size_16, size_20, size_24, size_28;
JTextArea jTextArea;
String text;
MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Untitled - CodePad");
// this is for shortcut keys
MenuShortcut menuShortcut_new_item1 = new MenuShortcut(KeyEvent.VK_N);
MenuShortcut menuShortcut_item2 = new MenuShortcut(KeyEvent.VK_O);
MenuShortcut menuShortcut_item3 = new MenuShortcut(KeyEvent.VK_S);
MenuShortcut menuShortcut_item4 = new MenuShortcut(KeyEvent.VK_X);
MenuShortcut menuShortcut_item5 = new MenuShortcut(KeyEvent.VK_C);
MenuShortcut menuShortcut_item6 = new MenuShortcut(KeyEvent.VK_V);
MenuShortcut menuShortcut_item7 = new MenuShortcut(KeyEvent.VK_T);
MenuShortcut menuShortcut_item8 = new MenuShortcut(KeyEvent.VK_A);
// -------------------------------------------
// setting icon
Image icon = Toolkit.getDefaultToolkit().getImage(".//res//icon.png");
setIconImage(icon);
//
bar = new MenuBar(); // creating object of menubar and giving it reference
menu1 = new Menu("File");// creating object of menu as 'File' and giving it reference
menu2 = new Menu("Edit");// creating object of menu as 'Edit' and giving it reference
format_menu = new Menu("Format");// creating object of menu as 'Format' and giving it reference
font_size = new Menu("Font Size");// creating object of menu as 'Font Size' and giving it reference
theme = new Menu("Theme");// creating object of menu as 'Theme' and giving it reference
//// creating object of MenuItem and giving it reference,and Passing arguments
//// 'label','menushortcut'
new_item1 = new MenuItem("New", menuShortcut_new_item1);
item2 = new MenuItem("Open", menuShortcut_item2);
item3 = new MenuItem("Save", menuShortcut_item3);
item4 = new MenuItem("Exit", menuShortcut_item4);
item5 = new MenuItem("Copy", menuShortcut_item5);
item6 = new MenuItem("Paste", menuShortcut_item6);
item7 = new MenuItem("Cut", menuShortcut_item7);
item8 = new MenuItem("Select All", menuShortcut_item8);
// ------------------done--------------
// creating menuItem for font size menu
size_8 = new MenuItem("8");
size_12 = new MenuItem("12");
size_16 = new MenuItem("16");
size_20 = new MenuItem("20");
size_24 = new MenuItem("24");
size_28 = new MenuItem("28");
// -------------------done-------------------
// creating menuItem for theme menu
dracula = new MenuItem("Dracula");
queen = new MenuItem("Queen");
dawn = new MenuItem("Dawn");
light = new MenuItem("Light");
// creating menuItem for theme menu
// adding new_item1,2,3,4 to menu1 ,that is new,open,save,exit
menu1.add(new_item1);
menu1.add(item2);
menu1.add(item3);
menu1.add(item4);
// ------------------Done-------------------
// adding item5,6,7,8 to menu2 ,that is copy,paste,cut,and select all
menu2.add(item5);
menu2.add(item6);
menu2.add(item7);
menu2.add(item8);
// -------done---------------------------------------------------------
format_menu.add(font_size);// adding font_size menu to format menu so it becomes submenu
// adding MenuItems to font_size menu
font_size.add(size_8);
font_size.add(size_12);
font_size.add(size_16);
font_size.add(size_20);
font_size.add(size_24);
font_size.add(size_28);
// ---------done------------------------
// adding MenuItem to theme Menu-------
theme.add(dracula);
theme.add(queen);
theme.add(dawn);
theme.add(light);
// ---------done------------------------
jTextArea = new JTextArea();// adding jTextArea
jTextArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(jTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(scroll);
// adding menus to bar
bar.add(menu1);
bar.add(menu2);
bar.add(format_menu);
bar.add(theme);
setMenuBar(bar); // settingmenubar as bar
add(jTextArea);// adding text area
// declaring some colors using rgb
Color dracula_Color = new Color(39, 40, 34);
Color green_Color = new Color(166, 226, 41);
Color orange_Color = new Color(219, 84, 34);
Color queen_Color = new Color(174, 129, 219);
// setting default foreground color of jTextArea and setting font
jTextArea.setForeground(Color.BLUE);
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 15));
// setting size and location and visibility
setSize(1000, 600);
setLocationRelativeTo(null);
setVisible(true);
item2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
FileDialog dialog = new FileDialog(new Frame(), "Open", FileDialog.LOAD); // this will load the
// fileDialog
dialog.setVisible(true);// this will make dialog visible
String path = dialog.getDirectory() + dialog.getFile(); // this will select the path of selected file
// and put it into 'path'
setTitle(dialog.getFile() + " - CodePad");// this will set Title to selected file name and -CodePad
try {
FileInputStream fi = new FileInputStream(path);
byte b[] = new byte[fi.available()];
fi.read(b);
String str = new String(b); // this will store b in str
jTextArea.setText(str);// this will display text in 'str' in jTextArea
fi.close();// this will close fi
} catch (Exception e) {
System.out.println("Something went Wrong :(");
}
}
});
new_item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setTitle("Untitled - CodePad");
jTextArea.setText(" ");
}
});
item3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
FileDialog dialog = new FileDialog(new Frame(), "Save ", FileDialog.SAVE);
dialog.setVisible(true);
String path = dialog.getDirectory() + dialog.getFile();
setTitle(dialog.getFile() + "- CodePad");
try {
FileWriter fw = new FileWriter(path);
fw.write(jTextArea.getText());
fw.close();
} catch (Exception e) {
System.out.println("Something went Wrong :(");
}
}
});
item4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// setVisible(false);//this will make frame invisible
System.exit(0);
}
});
item5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
text = jTextArea.getSelectedText();// this will store selected text in to variable 'text'
}
});
item6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.insert(text, jTextArea.getCaretPosition()); // this will insert the text present in 'text'
// variable at the carret position
}
});
item7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
text = jTextArea.getSelectedText(); // this will copy the selected text
jTextArea.replaceRange("", jTextArea.getSelectionStart(), jTextArea.getSelectionEnd()); // this will put
// ""
// to selected
// text
}
});
item8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.selectAll(); // this will select all the text in jTextArea
}
});
// ------------------------------------------------------------------------
// --------------------------------------------------------------------------
size_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 8)); // this will change the size of text in
// jTextArea to 8
}
});
size_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 12));// this will change the size of text in
// jTextArea to 12
}
});
size_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 16));// this will change the size of text in
// jTextArea to 16
}
});
size_20.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));// this will change the size of text in
// jTextArea to 20
}
});
size_24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 24));// this will change the size of text in
// jTextArea to 24
}
});
size_28.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 28));// this will change the size of text in
// jTextArea to 28
}
});
// --------------------------------------------------------------------------
dracula.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setBackground(dracula_Color);// this will backgound to dracula
jTextArea.setForeground(green_Color);// this will set foregrounf to green
}
});
queen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setBackground(dracula_Color);
jTextArea.setForeground(queen_Color);
}
});
dawn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setBackground(Color.WHITE);
jTextArea.setForeground(orange_Color);
}
});
light.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jTextArea.setBackground(Color.WHITE);
jTextArea.setForeground(Color.BLUE);
}
});
// --------------------------------------------------------------------------
}
}
// ---------------------------------------
public class CodePad_updated {
public static void main(String[] args) {
new MyFrame();// object
}
}
最佳答案
您是 Swing 新手吗?我没有看到您设置内容 Pane 。我也没有看到您在 actionListeners 中使用 @Override 命令。
正如我发现的一些可疑的事情一样。我通常创建一个新的 JFrame 而不是扩展它。我认为扩展 JFrame 是一种不好的做法。但这并不是普遍的观点。然后,您将向框架添加一个面板并将其设置为 contentPane。然后您可以开始将所有内容添加到面板中,包括其他面板以帮助进行 UI 布局。文本字段是否显示?因为我怀疑事实并非如此。此外,您还需要将 ScrollPane 添加到 contentPane,而不是 Frame。我建议删除帖子中代码中与您的问题无关的所有内容,即与当前主题无关的所有内容。
编辑:您是否尝试过将 textArea 添加到滚动 Pane ?它看起来像这样。
JTextArea text = new JTextArea();
JScrollPane newScroll = new JScrollPane(text);
这对你有帮助吗?
关于java - 为什么 JScrollpane 没有添加到我的文本编辑器中的 JTextArea 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69587782/
我有一个 JScrollPane,它的内容 Pane 有一个 JPanel。我向该 JPanel 添加了较小的 JPanel,正如预期的那样,如果我添加太多 JPanel,将会出现一个垂直滚动条。 问
我正在尝试在另一个 JScrollPane 中添加一个 JScrollPane。内部 JScrollPane 只会水平滚动,外部只会垂直滚动。 在这张图中,您可以看到添加 mainPanel 时水平滚
我正在尝试创建一个带有滚动条的容器,并且容器内部有两个内部面板。顶部内面板内还有另一个 JScrollPane。 但目前我遇到的问题是,当我的顶部内面板太长(宽度)时,顶部内面板内的滚动条被禁用,我只
你们能让我知道禁用水平滚动条的最佳方法是什么吗? 我有 宽度:100% 和 高度:280px 的 div。当我们有很长的连续文本(没有任何空格)时,我们会显示一个水平滚动条。 顺便说一句,我正在使用
我注意到这个问题主要出现在 OS 10.5.8 上的 Firefox 3.6.6 上,Safari 上偶尔也会出现这种情况(准备好惊讶的表情 - IE 实际上每次都工作正常 - 什么?!)。 我的网址
所以我有一堆JTable。每个JTable 都位于JScrollPane 内。然后,我将每个 JScrollPane 添加到 JPanel 中。然后,我将此 JPanel 添加到 JScrollPan
我在 JScrollPane 上放置了多个 JPanel。现在我有了它,所以如果你的鼠标在框架之外,那么它就不会拖动 JPanels。 当我在一个方向上移动组件时,我需要让它滚动。 (例如,如果我
有什么区别 JScrollPane.getViewportBorderBounds() and JScrollPane.getViewport() and JscrollPane.getVisible
此应用程序适用于触摸屏。我只需要仅当用户触摸 JScrollPane 区域时 JScrollPane 的滚动条可见。 我是 GUI 和 swing 的新手。这会很有帮助,我不明白是什么,或者如果在其他
出于布局目的,我需要在滚动内容底部和容器底部之间放置 15px 的空间:div class="scroll-pane" . 造型容器 .scroll-pane { padding-bottom:15p
我需要一个可以显示很多图像的程序,并且我需要一个可以滚动的窗口。我阅读了文档并在论坛上进行了搜索,但仍然没有成功。我尝试使用 JScrollPane 和 JFrame,如下所示。 JScrollPan
我今天遇到了这个新事物,但我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板即可;但为什么我不能直接添加表格到滚动 Pane ,为什么我必须调用 setviewportview(
我今天遇到了这个新事物,我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板即可;但是为什么我不能直接添加表格到滚动 Pane ,为什么我必须调用 setviewportview(
我一直在尝试缩小 JScrollPane 的内容宽度,例如。我已将 HorizontalScrollBarPolicy 设置为 NEVER,但这最终导致没有 ScrollBar 出现并且内容不再显
所以,在下面的代码中,我在左侧有一个 JTextArea。右上角的 JScrollPane 看起来不错。使用相同的代码,我还在右下侧添加了一个 JScrollPane,但是尽管代码相同,但保存了首选大
我在 div 上使用 JScrollPane 来滚动它。 div 包含一个自写的 javascript 代码,它从我的 tumblr 提要中获取最后几篇文章。但是,即使滚动 Pane 显示正常,但由于
请看下面的代码块 import java.awt.Color; import java.awt.Dimension; import javax.swing.JFrame; import javax.s
我在 Swing 中制作了一个简单的 GUI,其中在 JScrollPane 内、JSPlitPane 内、JPanel 内、....在 JFrame 内有一个大 JPanel(显示大 Buffere
我有一个带滚动条的 JPanel,我想向它添加很多 JLabel。但是滚动条不起作用。我无法使用滚动条,即使面板已满,它也不会滚动。这是我的代码: import java.awt.*; import
我正在尝试在 JScrollPane 中添加 2 个图像。第一个图像是背景,第二个图像与第一个图像重叠。当我运行我的程序时,问题只显示第二张图像! 请帮忙 ImageIcon ii = new Ima
我是一名优秀的程序员,十分优秀!