如何在 Swing 中制作合适的 Table 以及使用合适的布局将所有元素放在各自的位置?

我正在研究网络爬虫并分几个阶段实施。

此阶段获取网站上提到的所有链接,并在 JTable 中显示带有标题的网页的有效链接。

但是,我希望在我在要解析的 TextField 中输入 URL 之前该表已经存在(这是此分配的要求)。

另外,有没有办法修复当前布局(Macintosh 窗口的第一张图片) 当前 Swing 窗口 ,以便我可以在指定位置正确显示所有 JFrame 元素,如下图所示:

所有元素所在的所需摆动窗口

http://img3.sycdn.imooc.com/647c556b00014dea06460417.jpg

我曾尝试使用空布局来解决布局问题,这只会让任务变成一场噩梦,而且表格会在我眼前消失。

在我开始输入链接之前,我找不到让表格存在的方法。

当我运行作业测试工具给出的测试时,它显示“找不到合适的表元素”。

发生这种情况是因为它甚至需要在将测试用例 URL 输入文本字段进行解析之前检测活动表。


青春有我
浏览 104回答 2
2回答

浮云间

不要在JButton的ActionPerformed事件中创建JScrollPane和JTable。但是,如果您愿意,可以从该事件重新启用 JTable,并将链接应用到 JTable 的模型。如果可以,请在单独的类中创建您的表单。这清理了很多东西,因为它使表单代码杂乱无章,远离手头的主要任务……页面抓取链接。表单类可能如下所示:package webcrawler;public class Form extends javax.swing.JFrame {private static final long serialVersionUID = 101101L;// Member Variables for Form Components&nbsp;public javax.swing.JTextField urlText;public javax.swing.JButton extract;public javax.swing.JLabel title;public javax.swing.JTable table;private javax.swing.JLabel jLabel1;private javax.swing.JPanel jPanel1;private javax.swing.JPanel jPanel2;private javax.swing.JScrollPane scrollPane;private javax.swing.JLabel titleLabel;public Form() {&nbsp; &nbsp; initializeFormComponents();&nbsp; &nbsp; setLookAndFeel();&nbsp; &nbsp; openForm();}private void setLookAndFeel() {&nbsp; &nbsp; /* Set the Nimbus look and feel if you have it&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;or even like it. Use whatever you like&nbsp; &nbsp;*/&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("Nimbus".equals(info.getName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; javax.swing.UIManager.setLookAndFeel(info.getClassName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex.getMessage());&nbsp; &nbsp; }}private void openForm() {&nbsp; &nbsp; /* Create and display the form */&nbsp; &nbsp; java.awt.EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}private void initializeFormComponents() {&nbsp; &nbsp; jPanel1 = new javax.swing.JPanel();&nbsp; &nbsp; jLabel1 = new javax.swing.JLabel();&nbsp; &nbsp; urlText = new javax.swing.JTextField();&nbsp; &nbsp; extract = new javax.swing.JButton();&nbsp; &nbsp; jPanel2 = new javax.swing.JPanel();&nbsp; &nbsp; scrollPane = new javax.swing.JScrollPane();&nbsp; &nbsp; table = new javax.swing.JTable();&nbsp; &nbsp; titleLabel = new javax.swing.JLabel();&nbsp; &nbsp; title = new javax.swing.JLabel();&nbsp; &nbsp; setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);&nbsp; &nbsp; setTitle("Web Crawler");&nbsp; &nbsp; setAlwaysOnTop(true);&nbsp; &nbsp; jLabel1.setFont(new java.awt.Font("sansserif", 1, 12)); // NOI18N&nbsp; &nbsp; jLabel1.setText("URL:");&nbsp; &nbsp; urlText.setName("UrlTextField"); // NOI18N&nbsp; &nbsp; extract.setText("Parse");&nbsp; &nbsp; extract.setName("RunButton"); // NOI18N&nbsp; &nbsp; javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);&nbsp; &nbsp; jPanel1.setLayout(jPanel1Layout);&nbsp; &nbsp; jPanel1Layout.setHorizontalGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel1Layout.createSequentialGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(urlText, javax.swing.GroupLayout.PREFERRED_SIZE, 667, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(extract, javax.swing.GroupLayout.DEFAULT_SIZE, 84, Short.MAX_VALUE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap())&nbsp; &nbsp; );&nbsp; &nbsp; jPanel1Layout.setVerticalGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel1Layout.createSequentialGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(jLabel1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(urlText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(extract))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))&nbsp; &nbsp; );&nbsp; &nbsp; table.setModel(new javax.swing.table.DefaultTableModel(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Object[][]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {null, null},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {null, null},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {null, null},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {null, null}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "URL", "TITLE"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; private static final long serialVersionUID = 101110L;&nbsp; &nbsp; &nbsp; &nbsp; Class[] types = new Class[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; java.lang.String.class, java.lang.String.class&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; boolean[] canEdit = new boolean[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false, false&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Class getColumnClass(int columnIndex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return types[columnIndex];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean isCellEditable(int rowIndex, int columnIndex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return canEdit[columnIndex];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; table.setName("TitlesTable");&nbsp; &nbsp; table.setEnabled(false);&nbsp; &nbsp; table.setGridColor(new java.awt.Color(198,195,195));&nbsp; &nbsp; table.setInheritsPopupMenu(true);&nbsp; &nbsp; table.setRowHeight(20);&nbsp; &nbsp; table.setShowHorizontalLines(true);&nbsp; &nbsp; table.setShowVerticalLines(true);&nbsp; &nbsp; scrollPane.setViewportView(table);&nbsp; &nbsp; titleLabel.setFont(new java.awt.Font("sansserif", 1, 12));&nbsp; &nbsp; titleLabel.setText("Title:");&nbsp; &nbsp; title.setFont(new java.awt.Font("sansserif", 1, 12));&nbsp; &nbsp; title.setText("None");&nbsp; &nbsp; title.setName("TitleLabel");&nbsp; &nbsp; javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);&nbsp; &nbsp; jPanel2.setLayout(jPanel2Layout);&nbsp; &nbsp; jPanel2Layout.setHorizontalGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel2Layout.createSequentialGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(scrollPane)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel2Layout.createSequentialGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGap(18, 18, 18)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGap(0, 0, Short.MAX_VALUE)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap())&nbsp; &nbsp; );&nbsp; &nbsp; jPanel2Layout.setVerticalGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(titleLabel)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(title))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addContainerGap())&nbsp; &nbsp; );&nbsp; &nbsp; javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());&nbsp; &nbsp; getContentPane().setLayout(layout);&nbsp; &nbsp; layout.setHorizontalGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)&nbsp; &nbsp; );&nbsp; &nbsp; layout.setVerticalGroup(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addGroup(layout.createSequentialGroup()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))&nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; pack();&nbsp; &nbsp; }}然后您的 WebCrawler 类可能如下所示:package webcrawler;import javax.swing.*;import javax.swing.table.DefaultTableModel;import javax.swing.table.TableModel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.nio.charset.StandardCharsets;import java.util.Iterator;import java.util.Map;import java.util.SortedMap;import java.util.TreeMap;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.table.JTableHeader;import javax.swing.table.TableCellRenderer;public class WebCrawler {private static final long serialVersionUID = 101011L;private Form form;String LINE_SEPARATOR = System.getProperty("line.separator");public static void main(String[] args) {&nbsp; &nbsp; // To eliminate the need for statics. I Don't want it right now.&nbsp;&nbsp; &nbsp; new WebCrawler().startApp(args);}private void startApp(String[] args) {&nbsp; &nbsp; form = new Form();&nbsp; &nbsp; form.extract.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearJTable();&nbsp; // Clear the JTable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String url = form.urlText.getText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Get url from JTextField */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url = url.replaceAll("^\"+ \"+$", "");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final InputStream inputStream = new URL(url).openStream();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String nextLine;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((nextLine = reader.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(nextLine);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append(LINE_SEPARATOR);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final String siteText = stringBuilder.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pattern pattern = Pattern.compile("<title>(.+?)</title>", Pattern.CASE_INSENSITIVE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Matcher matcher = pattern.matcher(siteText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String titleString = matcher.find() ? matcher.group(1) : "null";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.title.setText(titleString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SortedMap<String, String> links = new TreeMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pattern pTag = Pattern.compile("(?i)<a([^>]+)>(.+?)</a>", Pattern.CASE_INSENSITIVE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pattern pLink = Pattern.compile("\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))", Pattern.CASE_INSENSITIVE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Matcher mTag = pTag.matcher(siteText);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (mTag.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String href = mTag.group(1);&nbsp; &nbsp; &nbsp;// get the values of href&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String linkElem = mTag.group(2); // get the text of link Html Element&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Matcher mLink = pLink.matcher(href);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (mLink.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String link = mLink.group(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link = link.substring(1, link.length() - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern = Pattern.compile("http", Pattern.CASE_INSENSITIVE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matcher = pattern.matcher(link);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; link = String.join("", url, link);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //URL urlValidator = new URL(link);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (urlValidator.getContent().equals("text/html")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; links.put(link, linkElem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println("Exception encountered at " + link);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.table.setEnabled(true);&nbsp; &nbsp; // Enable JTable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.table.setModel(toTableModel(links));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(ex);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Is there anything in the table?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (form.table.getModel().getRowCount() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // NOPE...So Disable JTable.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; form.table.setEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; form.urlText.requestFocus();}private void clearJTable() {&nbsp; &nbsp; DefaultTableModel tableModel = (DefaultTableModel) form.table.getModel();&nbsp; &nbsp; while (tableModel.getRowCount() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < tableModel.getRowCount(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tableModel.removeRow(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Reset 4 blank rows in the table for looks :)&nbsp;&nbsp; &nbsp; form.table.setModel(new DefaultTableModel(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Object[][]{{null, null}, {null, null}, {null, null},{null, null}},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[]{"<html><font color=blue><b>U R L</b></font></html>",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"<html><font color=red><b>T I T L E</b></font></html>"}));&nbsp; &nbsp; // Yes...you can use basic HTML in a JTable Header.}public TableModel toTableModel(Map<?, ?> map) {&nbsp; &nbsp; DefaultTableModel model = new DefaultTableModel(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Object[]{"URL", "TITLE"}, 0&nbsp; &nbsp; );&nbsp; &nbsp; for (Iterator it = map.entrySet().iterator(); it.hasNext();) {&nbsp; &nbsp; &nbsp; &nbsp; Map.Entry entry = (Map.Entry) it.next();&nbsp; &nbsp; &nbsp; &nbsp; model.addRow(new Object[]{entry.getKey(), entry.getValue()});&nbsp; &nbsp; }&nbsp; &nbsp; return model;}}

蓝山帝景

我希望在输入 URL 之前表格已经存在然后,您需要在创建所有其他组件并将其添加到框架的同时创建一个空表并将其添加到框架。您需要为 JTable 创建一个实例变量才能修改其模型。将表格添加到框架的代码类似于:add(title, BorderLayout.LINE_START);DefaultTableModel model = new DefaultTableModel (new Object[] { "URL", "Title" }, 0);table = new JTable( model );add(new JScrollPane(table), BorderLayout.CENTER);所以现在当您启动应用程序时,您将看到一个只有列的空表。您的方法中的代码toTableModel(...)现在变成如下所示://DefaultTableModel model = new DefaultTableModel (new Object[] { "URL", "Title" }, 0);DefaultTableModel model = (DefaultTableModel)table.getModel();model.setRowCount(0);这只是删除模型中的所有行,因此您的循环代码可以添加每一行新数据。当然,您还可以删除当前创建 JTable 和 JScrollPane 的 ActionListener 中的代码,因为不再需要它。另外,有没有办法修复当前布局您的嵌套面板具有不同的布局管理器,以达到您想要的效果。例如,您希望表格能够根据表格中的数据增长/缩小,因此应该将其添加到CENTER您的BorderLayout.但是,顶部到两行是固定的,它们应该添加到子面板中。然后这个面板将被添加到PAGE_START你的BorderLayout. Maybe a verticalBoxLayout` 中。然后每一行都是另一个子面板。第一行可以是带有水平的面板BoxLayout,第二行可以是带有FlowLayout.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java